Java 我需要将一个变量传递到线程中

Java 我需要将一个变量传递到线程中,java,multithreading,Java,Multithreading,这是我的程序的一个分条版本,突出显示了我需要在它睡眠时传递的问题,但环顾四周后,我似乎无法传递它,我只能找到传递到runnable的信息。尝试在课堂下运行,你有什么问题吗 Thread thread1; thread1 = new Thread() { public void run() { try { Thread.sleep(1700); } catch (InterruptedException ex) {

这是我的程序的一个分条版本,突出显示了我需要在它睡眠时传递的问题,但环顾四周后,我似乎无法传递它,我只能找到传递到runnable的信息。

尝试在课堂下运行,你有什么问题吗

Thread thread1;
thread1 = new Thread() {
    public void run() {
        try {
            Thread.sleep(1700);
        } catch (InterruptedException ex) {
            Logger.getLogger(TestThread.class.getName()).log(Level.SEVERE, null, ex);
        }
       System.out.println("testing");
    }
};
Thread thread2;
thread2 = new Thread() {
    public void run() {
        try {
            // ... your code here
            Thread.sleep(1000);
            System.out.println("testing");
        } catch (InterruptedException ex) {
            Logger.getLogger(TestThread.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
};
thread1.start();
thread2.start();
System.out.println("testing");

您可以尝试创建一个线程工厂,该工厂需要睡眠时间和自定义代码来执行:

public class TestThread {

    volatile static int time = 1700;

    public static void main(String[] args) {


        Thread thread1 = new Thread() {
            @Override
            public void run() {
                try {
                    Thread.sleep(time);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Thread1 Sleeped for : " + time + " millis");
            }
        };
        thread1.start();

        Thread thread2 = new Thread() {
            @Override
            public void run() {
                try {
                    Thread.sleep(time);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Thread2 Sleeped for : " + time + " millis");
            }
        };
        thread2.start();
    }

}

“我只能找到有关传递到runnable的信息。”您认为这有什么区别吗?
线程
可以被视为与
runnable
非常相似。但是,如果您想真正使用
可运行的
,只需按照您的示例将线程设置为这样:
thread1=newthread(myRunnable)
。这实际上比子类化线程(如您所做的)要好,因为它更灵活,而且它使用聚合而不是继承(这是首选方法)。我无法理解它/使它工作。我设法编辑并举例,可以将其传递到runnable,但不能在run下使用,这意味着我不能像上面所说的那样在我的线程的主要部分使用它。非常感谢Markspace,我现在就可以开始了。是否可以修改它以支持未知数量的线程以及每个线程的不同时间,如您所见,时间不是最终变量。因此,您可以根据需要多次修改它。此外,您可以在任何线程中使用此变量,但可能会被修改。每个线程可能无法立即看到该值(由于线程缓存)。因此,您可能需要将此变量设置为volatile
interface CodeExecutor {
    void execute();
}

static class ThreadFactory {
    public static Thread newThread(int sleepTime, CodeExecutor customCode) {
        return new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    customCode.execute();
                    Thread.sleep(sleepTime);
                    System.out.println("testing");
                } catch (InterruptedException ex) {
            Logger.getLogger(TestThread.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
}

public static void main(String[] args) {
    Thread thread1 = ThreadFactory.newThread(100, new CodeExecutor() {
        @Override
        public void execute() {
            // here goes your custom code
        }
    });

    thread1.start();
    // create other threads
}