AdSense

網頁

2019/3/18

Java 傳遞參數給執行緒的Runnable.run()

傳遞參數給執行緒Runnable.run()

public class App {
    
    public static void main(String[] args) {
        MyRun myRun = new MyRun(100);
        Thread t = new Thread(myRun);
        t.setName("MyRun");
        t.start();
    }
    
    
}

public class MyRun implements Runnable {

    private int count;
    
    public MyRun(int count) {
        this.count = count;
    }
    
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + ".count:" + count);
    }

}

印出結果如下

MyRun.count:100


參考:

沒有留言:

AdSense