Reputation: 333
i want to just use join() and isAlive function. I created 4 threads and initialized them in constructor. I started threads by using start method. It should call run() but the run method is not executing. Am i doing something wrong. Please tell me. Thank You in advance.
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());
}
}
Upvotes: 0
Views: 184
Reputation: 444
Use this edited Code `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.
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());
}
} `
Upvotes: 0
Reputation: 6721
The creation of the Thread object in your code is a Thread without a target.
You are using this:
t=new Thread(this.name);
You should initialize your thread with this:
t=new Thread(this, this.name);
It'll link the run method of your Runnable to the start method of your Thread.
Hope this helps!
Upvotes: 0
Reputation: 131556
t=new Thread(this.name);
is the problem.
You give a name to the thread but you don't provide the associated target Runnable
instance.
Just use this constructor :
public Thread(Runnable target, String name)
in this way :
t=new Thread(this,this.name);
Upvotes: 2