简体   繁体   中英

I need to pass an variable into a thread

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");

This is a striped down version of my program and highlights the problem i need to pass in the time it sleeps for but after looking around i can't seem to get it passed in i can only find info about passing to runnable.

Try to run below class, do you have any issue with it

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();
    }

}

You can try creating a thread factory that takes sleep time and your custom code to execute:

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM