jQuery in Action, 2nd edition*
The moose likes Threads and Synchronization and the fly likes wait method doesn't working. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "wait method doesn Watch "wait method doesn New topic
Author

wait method doesn't working.

Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52


Why method wait() doesn't work ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18529
    
  40

Dawid Skrzypczynski wrote:
Why method wait() doesn't work ?


The wait method causes the current thread to wait for notification. It doesn't cause the thread, represented by the thread object that you are using to wait. Notice that it is a method of the object class, and not the thread class.

As for why the instance is needed, it is related to how condition variables work.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18529
    
  40

Dawid Skrzypczynski wrote:


Maybe, but the method is deprecated. And the reason it is deprecated is... under certain conditions it can suspend the thread, along with the JVM locks that it owns, and cause the whole JVM to no longer work.

Henry
Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52
How should I interrupt the thread?

I will add only that I would like to resume later this thread
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18529
    
  40

Dawid Skrzypczynski wrote:How should I interrupt the thread?

I will add only that I would like to resume later this thread


The only safe way to do this is cooperatively. There needs to be communication between the two threads -- maybe using flags, wait/notify, and/or interrupts. The targeted thread needs to monitor requests and be coded to pause and restart as needed.

Henry
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
Question too difficult for "beginning". Moving thread.
Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52
i wrote something like this:

This code doesn't working
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

First of all don't use someBoolean == false, instead use !someBoolean.

The "problem" is that the two threads each hold a copy of the pauze variable (disregarding the fact that you use the lock only in one place).
Their are a couple of ways of synchronize those. I would advise you to read this. Multi-threaded programming without the proper knowledge of threading can and will lead to some horrible bugs.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 407
    
    1



change to ...



"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52
Thanks for the replay Chris but after changing program still doesn't work properly.
I tried to do it slightly differently but the program still doesn't work. I have one question.

1) Why in line 53 the program show me that they are three threads. I think that should be two. The main thread and t thread.

I really don't understand why after changing the value of variable pause the Thread is not blocked...

Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52
Ok I discovered that this line:

induces new thread but how can i control this particular thread ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18529
    
  40

Dawid Skrzypczynski wrote:Ok I discovered that this line:

induces new thread but how can i control this particular thread ?


Back to my original response -- the only safe way to do this is cooperatively. Obviously, if you don't have access to change the code in this new thread, then you can't.

Henry
Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52
Thank you all for your reply.
If i can't blocked the thread induced by the constructor Player that the only solution is use the suspend and resume methods.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18529
    
  40

Dawid Skrzypczynski wrote:Thank you all for your reply.
If i can't blocked the thread induced by the constructor Player that the only solution is use the suspend and resume methods.


In the past, I noticed that debbuggers and IDEs are the most likely culprits to break with suspend and resume. Now that it has been deprecated for so long, I would not be surprised if other setups may break.

But, if you have no choice, you have no choice. Hope it works.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wait method doesn't working.
 
Similar Threads
Reading Data from a file and extracting the data
HTTP response code: 403 on http://translate.google.com/translate_tts?q=hello%20world
File Not Found Error
Using FileInputStream with a relative location not working with JAR
Reading Reg exp from text file give error