[英]run() method not executing using start()
我只想使用join()和isAlive函数。 我创建了4个线程,并在构造函数中对其进行了初始化。 我通过使用start方法启动了线程。 它应该调用run(),但run方法未执行。 难道我做错了什么。 请告诉我。 先感谢您。
class MultiThreads implements Runnable {
String name;
Thread t;
MultiThreads(String tname) {
name=tname;
t=new Thread(this.name);
System.out.println("Thread name: " + t);
t.start(); //executing run()
}
public void run(){
try{
for(int i=1;i<11;i++){
System.out.println("Thread-"+name+ ": " + i);
t.sleep(500);
}
}catch(Exception ie){
System.out.println("An error has occurred");
}
}
}
public class JoinAlive {
public static void main(String args[]) {
//Creating New Threads by calling constructor.
MultiThreads t1=new MultiThreads("One");``
MultiThreads t2=new MultiThreads("Two");
MultiThreads t3=new MultiThreads("Three");
MultiThreads t4=new MultiThreads("Four");
System.out.println();
System.out.println("Thread-One active: " + t1.t.isAlive());
System.out.println("Thread-Two active: " + t2.t.isAlive());
System.out.println("Thread-Three active: " + t3.t.isAlive());
System.out.println("Thread-Four active: " + t4.t.isAlive());
try{
System.out.println();
System.out.println(" Waiting for One");
t1.t.join();
System.out.println(" Waiting for Two");
t2.t.join();
}catch(InterruptedException ie){
System.out.println("An error occurred");
}
System.out.println();
System.out.println("Thread-One active: " + t1.t.isAlive());
System.out.println("Thread-Two active: " + t2.t.isAlive());
System.out.println("Thread-Three active: " + t3.t.isAlive());
System.out.println("Thread-Four active: " + t4.t.isAlive());
}
}
t=new Thread(this.name);
是问题。
您为线程命名,但没有提供相关的目标Runnable
实例。
只需使用以下构造函数:
public Thread(Runnable target, String name)
通过这种方式 :
t=new Thread(this,this.name);
在代码中创建Thread对象是没有目标的Thread。
您正在使用此:
t=new Thread(this.name);
您应该使用以下方法初始化线程:
t=new Thread(this, this.name);
它将Runnable的run方法链接到Thread的start方法。
希望这可以帮助!
使用此经过编辑的Code`类MultiThreads实现Runnable {
String name;
Thread t;
MultiThreads(String tname) {
name=tname;
t=new Thread(this.name);
System.out.println("Thread name: " + t);
t.start(); //executing run()
}
public void run(){
try{
for(int i=1;i<11;i++){
System.out.println("Thread-"+name+ ": " + i);
t.sleep(500);
}
}catch(Exception ie){
System.out.println("An error has occurred");
}
}
}
公共课程JoinAlive {
public static void main(String args[]) {
//Creating New Threads by calling constructor.
Thread t1=new Thread(new MultiThreads("One"));
Thread t2=new Thread(new MultiThreads("Two"));
Thread t3=new Thread(new MultiThreads("Three"));
Thread t4=new Thread(new MultiThreads("Four"));
System.out.println();
System.out.println("Thread-One active: " + t1.isAlive());
System.out.println("Thread-Two active: " + t2.isAlive());
System.out.println("Thread-Three active: " + t3.isAlive());
System.out.println("Thread-Four active: " + t4.isAlive());
try{
System.out.println();
System.out.println(" Waiting for One");
t1.join();
System.out.println(" Waiting for Two");
t2.join();
}catch(InterruptedException ie){
System.out.println("An error occurred");
}
System.out.println();
System.out.println("Thread-One active: " + t1.isAlive());
System.out.println("Thread-Two active: " + t2.isAlive());
System.out.println("Thread-Three active: " + t3.isAlive());
System.out.println("Thread-Four active: " + t4.isAlive());
}
}`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.