可以通过将runnable实现为来创建线程
public class Program implements Runnable{
public void run(){
System.out.println("Thread in progress");
}
public static void main(string args[]){
Program p1 = new Program();
new Thread(p1).start();
//we can also use
(new Thread(new Program())).start();
}
}
一种方法优先于另一种方法吗?
Program p1 = new Program();
new Thread(p1).start(); // this thread and the next thread share the same Runnable instance
// So, they share the instance level fields also. Thus either one of them can change the state of p1
new Thread(p1).start();
//we can also use
(new Thread(new Program())).start();
// This one creates a new instance of Program/ Runnable, thus does not share the same object.
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句