• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

New to threads, need some help

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();

}

}
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do u mean by posting the code tags?
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic