Thread and wait simple question

H

hilz

Hi group.
I am trying to learn how to make a thread wait until another thread
terminates.
I have created a simple example that compiles and runs (please see at
the end of this message, and beware of text-wrapping!).

I have two threads t1 and t2.
t1 prints a message every second, and terminates after 10 messages.
t2 should wait till t1 has ended and then print a message to inform me
of that.
After some playing around, i could not get it to work. i think i should
put something at the place where i put the following comment:

//I think i need to put something here!

One of the things i tried was to put "t1.wait();" but that gives a
compile error that t1 should be declared final. So I declared t1 as
final, and that did not help. the message from t2 still appears before
any messages from t1.


Any help is greatly appreciated.
thanks
hilz.






public class NewClass {

public NewClass() {
Thread t1 = new Thread(new Runnable(){

public void run(){
try{
for (int i=0;i<10;i++){
Thread.sleep(1000);
System.out.println("Thread t1: "+i);
}
}catch(Exception exp){exp.printStackTrace();}

}
});

Thread t2 = new Thread(new Runnable(){
int counter=0;
public void run(){
try{
//I think i need to put something here!
System.out.println("This message comes to you from
thread t2 to inform you that t1 has terminated.");

}catch(Exception exp){exp.printStackTrace();}

}
});

t1.start();
t2.start();
}
public static void main(){
new NewClass();


}

}
 
A

Andrew Thompson

I have created a simple example that compiles and runs

You sure have, it is almost a 'great' example of an SSCCE.
..(please see at
the end of this message, and beware of text-wrapping!).

Why? Or rather, why should we beware of text wrapping, when you
are obviously aware of the problem and could have done something
about it yourself?

The only line of your example that breaks (with my default 'wrap
at 72 char' newsreader) is this one..
System.out.println("This message comes to you from
thread t2 to inform you that t1 has terminated.");

Simple solution.
System.out.println("This message comes to " +
"you from thread t2 to inform you that " +
"t1 has terminated.");

Also note that you do not need to use so many spaces to indent
successively. 3 spaces is common, 2 is even enough.
(Hurrah for not posting 'tabs'!)

As to the problem? I suspect you need to 'join()' the first thread..

HTH
 
A

Andrew Thompson

You sure have, it is almost a 'great' example of an SSCCE.

Oh! (silly grin)

And now that I actually *run* your example I noticed something else..
public static void main(){

That ain't no 'main' method, which has a very specific signature..

public static void main(String[] args){

Note that the String array of arguments is required,
even if your application has no use for arguments.

HTH
 
S

shakah

hilz said:
Hi group.
I am trying to learn how to make a thread wait until another thread
terminates.
I have created a simple example that compiles and runs (please see at
the end of this message, and beware of text-wrapping!).

I have two threads t1 and t2.
t1 prints a message every second, and terminates after 10 messages.
t2 should wait till t1 has ended and then print a message to inform me
of that.
After some playing around, i could not get it to work. i think i should
put something at the place where i put the following comment:

//I think i need to put something here!

One of the things i tried was to put "t1.wait();" but that gives a
compile error that t1 should be declared final. So I declared t1 as
final, and that did not help. the message from t2 still appears before
any messages from t1.


Any help is greatly appreciated.
thanks
hilz.






public class NewClass {

public NewClass() {
Thread t1 = new Thread(new Runnable(){

public void run(){
try{
for (int i=0;i<10;i++){
Thread.sleep(1000);
System.out.println("Thread t1: "+i);
}
}catch(Exception exp){exp.printStackTrace();}

}
});

Thread t2 = new Thread(new Runnable(){
int counter=0;
public void run(){
try{
//I think i need to put something here!
System.out.println("This message comes to you from
thread t2 to inform you that t1 has terminated.");

}catch(Exception exp){exp.printStackTrace();}

}
});

t1.start();
t2.start();
}
public static void main(){
new NewClass();


}

}

Your mention of "t2 should wait until t1 has ended" sounds like t2
should "join" t1, something like:

public class NewClass {
public static class T2
implements Runnable {
Thread tToJoin_ ;
public T2(Thread t) {
tToJoin_ = t ;
}
public void run() {
try {
System.out.println("T2: waiting for other thread") ;
tToJoin_.join() ;
System.out.println("T2: join() returned") ;
}
catch(InterruptedException x) {
x.printStackTrace() ;
}
}
}

public NewClass() {
Thread t1 = new Thread(new Runnable() {
public void run() {
try {
for(int i=0;i<10;i++) {
Thread.sleep(1000);
System.out.println("Thread t1: "+i);
}
} catch(Exception exp){exp.printStackTrace();}
}
});

Thread t2 = new Thread(new T2(t1)) ;

t1.start();
t2.start();
}

public static void main(String [] asArgs){
new NewClass();
}
}

There are other ways to accomplish this (e.g. coding to use
wait()/notify()
instead of join()).
 
H

hilz

shakah said:
There are other ways to accomplish this (e.g. coding to use
wait()/notify()
instead of join()).

sakah:
Thank you for the answer.
I ran your example and it worked very nicely.
I noticed, that once thread t2 joins t1, t2 has to wait for t1 to finish
before it can do anything after the join. right?

Now your other suggestion of using wait/notify will enable me to print
the message from t2 even before t1 terminates, i assume. right?

If the above is true, then the real situation that i am trying to apply
this to would be more like the wait/notify solution. For example have
thread t2 wait until t1 has printed 5 messages and then t2 prints a
message that says for example "t1 is 50% comlete".

I hope you won't mind showing me how to do it with the wait/notify
because it is really bugging me. I read the example about wait/notify on
the sun java website, but could not successfully apply it to my example.

thank you very much for your help.

hilz
 
A

Andrew Thompson

.
I hope you won't mind showing me how to do it with the wait/notify
because it is really bugging me. I read the example about wait/notify on
the sun java website,

What URL, specifically?
...but could not successfully apply it to my example.

Why not? What happened? What failed?
<http://www.physci.org/codes/javafaq.jsp#specific>
<http://www.physci.org/codes/javafaq.jsp#exact>

By the way, a better group for people begnning in Java is
<http://www.physci.org/codes/javafaq.jsp#cljh>.
 
S

shakah

hilz said:
sakah:
Thank you for the answer.
I ran your example and it worked very nicely.
I noticed, that once thread t2 joins t1, t2 has to wait for t1 to finish
before it can do anything after the join. right?

Now your other suggestion of using wait/notify will enable me to print
the message from t2 even before t1 terminates, i assume. right?

If the above is true, then the real situation that i am trying to apply
this to would be more like the wait/notify solution. For example have
thread t2 wait until t1 has printed 5 messages and then t2 prints a
message that says for example "t1 is 50% comlete".

I hope you won't mind showing me how to do it with the wait/notify
because it is really bugging me. I read the example about wait/notify on
the sun java website, but could not successfully apply it to my example.

thank you very much for your help.

hilz

Your assumptions of join() causing t2 to wait for t1 to exit, and
wait/notify acting differently are correct. I don't use wait/notify too
much myself (at least directly), but here's a trivial example that
compiles & runs (please be aware that I'm really not sure it is
"correct" in the multithread sense):

public class NewClass2 {
public static class T1
implements Runnable {
T2 t2_ ;
public T1(T2 t2) {
t2_ = t2 ;
}
public void run() {
try {
for(int i=0;i<10;i++) {
Thread.sleep(1000);
System.out.println("Thread t1: "+i);
if(5==i) {
t2_.notifyUs() ;
}
}
t2_.notifyUs() ;
}
catch(InterruptedException x) {
x.printStackTrace() ;
}
}
}

public static class T2
implements Runnable {
public T2() {
}
synchronized public void doWait()
throws InterruptedException {
wait() ;
}
synchronized public void notifyUs()
throws InterruptedException {
notify() ;
}
public void run() {
try {
System.out.println("T2: waiting for other thread to notify()
us") ;
doWait() ;
System.out.println("T2: wait() returned") ;
doWait() ;
System.out.println("T2: wait() returned") ;
}
catch(InterruptedException x) {
x.printStackTrace() ;
}
}
}

public NewClass2() {
T2 t2 = new T2() ;
Thread t1 = new Thread(new T1(t2)) ;

t1.start();
new Thread(t2).start() ;
}

public static void main(String [] asArgs){
new NewClass2();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top