Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

wait method doesn't working.

 
Dawid Skrzypczynski
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why method wait() doesn't work ?
 
Henry Wong
author
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How should I interrupt the thread?

I will add only that I would like to resume later this thread
 
Henry Wong
author
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50245
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question too difficult for "beginning". Moving thread.
 
Dawid Skrzypczynski
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i wrote something like this:

This code doesn't working
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


change to ...

 
Dawid Skrzypczynski
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I discovered that this line:

induces new thread but how can i control this particular thread ?
 
Henry Wong
author
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic