Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA获取线程中的可运行对象_Java_Multithreading - Fatal编程技术网

JAVA获取线程中的可运行对象

JAVA获取线程中的可运行对象,java,multithreading,Java,Multithreading,我正在使用这个标准构造函数: new Thread(myRunnable); 其中myRunnable是具有Runnable接口的自定义对象 在线程进程中,我需要访问那个可运行对象(告诉进程状态和进度),我该怎么做 如果我的对象是线程,我将使用: (MyThread) Thread.getCurrentThread() 但是使用runnable作为参数传递,我无法得到它 编辑 这是我的代码结构: public abstract class ProgressThread{

我正在使用这个标准构造函数:

   new Thread(myRunnable);
其中myRunnable是具有Runnable接口的自定义对象

在线程进程中,我需要访问那个可运行对象(告诉进程状态和进度),我该怎么做

如果我的对象是线程,我将使用:

   (MyThread) Thread.getCurrentThread()
但是使用runnable作为参数传递,我无法得到它

编辑

这是我的代码结构:

public abstract class ProgressThread{
    private float progress;     //... progress getter, setter...    
}

public class MyRunnable extends ProgressThread implements Runnable{
    public void run(){
        //starting processes...
        Job1 j1=new Job1().do();
        Job1 j2=new Job2().do();
    }

    private class Job1(){
        for(int i=0;i<10;i++){
            // do something
            float progress=i/10;
            // set job progress in thread
            Thread.getCurrentThread().getRUNNABLE().setProgress(progress);
        }
    }

}
公共抽象类ProgressThread{
私有浮点进程;/…进程获取程序、设置程序。。。
}
公共类MyRunnable扩展ProgressThread实现Runnable{
公开募捐{
//正在启动进程。。。
Job1 j1=新的Job1().do();
Job1 j2=新的Job2().do();
}
私有类Job1(){

对于(inti=0;i只需保留对runnable的引用,以便在需要时访问它

Runnable myRunnable = ...
new Thread(myRunnable).start();

// do what you wish with the runnable
myRunnable.foo();

只需保持对runnable的引用,以便在需要时可以访问它

Runnable myRunnable = ...
new Thread(myRunnable).start();

// do what you wish with the runnable
myRunnable.foo();

使用
this
?(或将其作为参数传递给需要访问runnable的其他对象)如何?我添加了一些代码以更好地解释我的问题…thanksHow关于使用
this
?(或将其作为参数传递给需要访问runnable的其他对象)?我添加了一些代码来更好地解释我的问题…谢谢。我理解我没有很好地解释。我添加了一些代码来解释为什么我需要引用runnable by thread。我理解我没有很好地解释。我添加了一些代码来解释为什么我需要引用runnable by thread。