• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

why wait for ever?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

the problem is that the thread t is waiting for ever,can someone tell me why?
thanks!
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi TC,


try to find out what will happen, if after the line
t.start();
the thread t will not start immediately, and the main thread will first continue.

And check the output.



Yours,
Bu.
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think whats happening here is before thread t start run method, main thread has already got the lock on object "t" as it is accessing synbchronized(t) block .So once main method finishes the execution of this block then thread t will start executing synchronized block on run method. Now when thread t executes wait() method,it remains in wait state forever as notify() method is already executed by the main thread.
If you want to confirm this then you can have sleep() method after the line t.start() & try the output.


t.start();
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
}
synchronized(t)
{
for(int i=0;i<10;i++)
System.out.println("main running");
 
tc king
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks venkatesh and Burkhard,just as venkatesh said.
thank you very much!
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
venkatesh pendharkar coded
Thread.sleep(1000);

Dont't give him 1000, give him only 99.

Threads don't have a trade union.
Bu.
 
Ranch Hand
Posts: 44
1
Oracle Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a possible solution. I've moved class MyThread to be an internal class to avoid having to write to class files.

class Test {
public static void main(String[] args)
{

MyThread t=new MyThread();
t.start();
synchronized(t)
{
for(int i=0;i<10;i++)
System.out.println("main running");
t.setOk();
}
}
public static class MyThread extends Thread
{
boolean ok = false;
public void run()
{
synchronized(this)
{
while (ok == false)
{

try
{
System.out.println("wait for main");
sleep(99);
}catch(Exception e)
{}
}
}
for(int i=0;i<10;i++)
System.out.println(Thread.currentThread().getName()+":"+i);
}

public void setOk()
{
ok = true;
}
}
}
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic