aspose file tools*
The moose likes Beginning Java and the fly likes Thread Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Thread Problem" Watch "Thread Problem" New topic
Author

Thread Problem

Prakash Pillai
Greenhorn

Joined: Feb 17, 2002
Posts: 12
Hi ,
I have doubt regarding the Creating of child threads.for example check out the following code.
class newthread implements Runnable
{
Thread t;
String name;
newthread (String tname)
{
name=tname;
t=new Thread(this,name);
System.out.println("New Thread :"+t);
t.start();
}
public void run()
{
try
{
for(int i=5;i> 0;i--)
{
System.out.println(name+":"+i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println(name+"interrupted");
}
System.out.println(name+"exiting");

}
}

class threadinterface
{
public static void main(String args[])
{
newthread t1 = new newthread("one");
newthread t2 = new newthread("two");
newthread t3 = new newthread("three");
System.out.println("Thread one is alive "+t1.t.isAlive());
System.out.println("Thread two is alive "+t2.t.isAlive());
System.out.println("Thread three is alive "+t3.t.isAlive());
try
{
System.out.println("Waiting for threads to finish");
t1.t.join();
t2.t.join();
t3.t.join();
}
catch(InterruptedException e)
{
System.out.println("Main Thread Interrupted");
}
System.out.println("Thread one is alive "+t1.t.isAlive());
System.out.println("Thread two is alive "+t2.t.isAlive());
System.out.println("Thread three is alive "+t3.t.isAlive());

}
}
In the above code i am creating 3 child threads from main thread.Now my q are as follows
1. Can again spwan/create child thread inside child thread.something like Nested try or Nested if ?
2.In inter thread communication i have 3 methods wait(),notify()and notifyall() .Now can wake up a spcific thread from wait state to Runnable state. ?
I need ure valuable advice guys.Thanx in advance.
From,
Prakash
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
1. Can again spwan/create child thread inside child thread.something like Nested try or Nested if ?

Yes. There is no restriction really where you spawn your new threads. All you need is an instance creation expression (new Thread()) and you can create as many threads as you want.

2.In inter thread communication i have 3 methods wait(),notify()and notifyall() .Now can wake up a spcific thread from wait state to Runnable state. ?

No, there is no way to pick a specific thread that gets notified. You can call notifyAll(), and all threads waiting on an object's lock , or just call notify(), in which case the thread scheduler determines which single thread to wake up.


Rob
SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Problem