• Post Reply Bookmark Topic Watch Topic
  • New Topic

New to threads, need some help

 
Jarrod Folino
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to threads and I can't seem to synchronize them properly. I want my program's output to be:

Hi
Bye
Hi
Bye
etc.

Instead of that output though I always get:
Hi
Hi
Bye
Hi
Hi
Bye
Hi
Hi
Hi
Bye
Hi
Hi
Hi
Bye

I can't understand why the byeThread won't finish properly, as it is supposed to loop 10 times but only does it 4 times.

public class Word extends Thread
{

public synchronized void sayHi()
throws InterruptedException
{
System.out.println("Hi");
sleep(500);
notify();
}

public synchronized void sayBye()
throws InterruptedException
{
wait();
sleep(500);
System.out.println("Bye");
}

}
///////////////////////////////////////////////////////////////////////
public class hiThread extends Thread
{
public Word a;

public hiThread(Word w)
{
a = w;
}

public void run()
{
try
{
for (int x=0; x < 10; x++)
{
a.sayHi();
}
}
catch (Exception e)
{

}

}

}
///////////////////////////////////////////////////////////////////////
public class byeThread extends Thread
{
public Word a;

public byeThread(Word w)
{
a =w;
}

public void run()
{
try
{
for (int x=0; x < 10; x++)
{
a.sayBye();
}
}
catch (Exception e)
{

}

}

}
/////////////////////////////////////////////////////////////////
public class Go
{

public static void main(String[] args)
{
Word myWord = new Word();
hiThread B = new hiThread(myWord);
byeThread C = new byeThread(myWord);

B.start();
C.start();

}

}
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some things.

First if you are going to post code use the code tags. Some people won't even bother replying if you don't.

Second the syncronize keyword means that only one thread can access that method at a time. It does nothing in your program. Threads are by nature asyncronous, there is no way to garuntee you will get hi, bye, hi, bye ect..

Third you should not extend Thread rather you should implement Runnable and use a Thread to run your Runnables.

Finally your Word class has no need to either extend Thread or implement Runnable. It is a basic simple class with two methods.

If you let the program run to completion it should print out Hi ten times, and Bye ten times, but there is no way to indicate order. If it is not completing there may be an exception being thrown, not sure, just a guess.
 
Henry Wong
author
Sheriff
Posts: 22529
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem 1, the hi thread is in a very tight loop. It basically calls a syncronized method, then immediately call the same synchronized method. The only way the bye thread can run, if it switches between iterations. The bye thread is also in a tight loop, but it gives up the lock when it executes wait.

Problem 2, when the notify is sent, there is no guarantee that the bye thread is actually waiting, or if another notification has already been sent. The reason the bye thread only run 4 times, is because 6 notifications has been lost.

Henry
 
Jarrod Folino
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do u mean by posting the code tags?
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jarrod Folino:
What do u mean by posting the code tags?
Put your code inside matching BBCode "code" tags: (remove spaces inside the square brackets):

[ code ] ... [ /code ]

You can type them or use the "Instant UBB Code" buttons immediately below the "Add Reply" button to produce output that maintains formatting, specifically indentation, and uses a monospaced font:
 
JshriMenon
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hullo there,
I think the reason behind "bye" being printed only 4 times or less than 10 times is that somewhere along the line it goes into wait and doesn't get notified at all to wake up as the hi thread has already executed and died.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!