wait 1 « Operation « Java Thread Q&A

Home
Java Thread Q&A
1.concurrency
2.Development
3.Exception
4.Notify
5.Operation
6.Socket
7.State
8.synchronize
9.Thread Safe
10.ThreadPool
Java Thread Q&A » Operation » wait 1 

1. How to make a Java thread wait for another thread's output?    stackoverflow.com

I'm making a Java application with an application-logic-thread and a database-access-thread. Both of them persist for the entire lifetime of the application. However, I need to make sure that the app thread ...

2. how to make a thread wait until a file is created in java    stackoverflow.com

i have a thread in java & i need this thread to wait until a file is exist or created. i write the following code:

while(!receivedDataFile.isFileExists("receiveddata.txt"))
       {
 ...

3. busy wait threads in java    stackoverflow.com

I have an assignment with threads and I cant have the threads go into busy wait state. How do I know if a thread is in blocking state or in busy wait? ...

4. Java, Don't wait for a thread to complete    stackoverflow.com

I need to find a way to spin off a thread from a static call and not wait for the thread to complete. Basically, a "fire and forget" approach. ...

5. Why should wait() always be called inside a loop    stackoverflow.com

I have read that we should always called a wait() from within a loop:

while (!condition) { obj.wait(); }
It works fine without a loop so why is that?

6. How to wait for a set of threads to complete?    stackoverflow.com

What is a way to simply wait for all threaded process to finish? For example, let's say I have:

public class DoSomethingInAThread implements Runnable{

    public static void main(String[] ...

7. IllegalMonitorStateException on wait() call    stackoverflow.com

I am using multi-threading in java for my program. I have run thread successfully but when I am using Thread.wait(), it is throwing java.lang.IllegalMonitorStateException. How can I make a thread wait until it ...

8. Java - How to know when thread is waiting?    stackoverflow.com

Is there any neat solution of knowing when a thread has been put into wait status? I am putting threads to wait and I notify them when i need it. But ...

9. waiting for results if necessary    stackoverflow.com

i'm creating a web page which contains a few dynamically generated images. in my page request handling i create all of the images and store them in a memory cache until they ...

10. Understanding wait()    stackoverflow.com

I created this silly program to play with wait()

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

      System.out.print("1 ");
   ...

11. Waiting for a thread to finish from a 3rd party library (OpenMap/Java)?    stackoverflow.com

My application currently has an interesting problem. This problem is with OpenMap but could probably be applied to any 3rd party library. In this particular example, our code needs to create our ...

12. Problem with waiting for condition in Java    stackoverflow.com

I want to make one thread that puts values to a queue when it gets empty and wait for this condition while it is not. Here's the code I've tried to ...

13. Is there a timeout for threads waiting on a synchronised method in Java?    stackoverflow.com

Is there a default timeout for threads waiting on a synchronised method in Java? Some threads in my app are not completing as expected. Is there anyway to check whether threads ...

14. Waiting on threads    stackoverflow.com

I have a method that contains the following (Java) code:

doSomeThings();
doSomeOtherThings();
doSomeThings() creates some threads, each of which will run for only a finite amount of time. The problem is that I don't ...

15. controlling threads flow    stackoverflow.com

I had a task to write simple game simulating two players picking up 1-3 matches one after another until the pile is gone. I managed to do it for computer choosing ...

16. How do I have 1 UI thread wait for another UI thread    stackoverflow.com

I have two UI threads. I want to make sure the first one is finishing running, and then run the other one. How could I do that? Thanks a lot!

UIJob uiJob = new UIJob("settext1") ...

17. Wait for one of several threads    stackoverflow.com

I have a java application where the main-thread starts 2 other threads. If one of these threads terminates, the main-thread may start another thread depending on the result of the terminated thread. Example: The ...

18. Most efficient method for a thread to wait for a specific time in Java    stackoverflow.com

I'm aware of this question here but I have a slightly different question. If I wish to hand-code via the various Thread methods myself (not via utility classes or Quartz) ...

19. Make parent thread wait till child thread completes or timeout    stackoverflow.com

I am running a Java process on unix. I need to run an external process which is spawned by the main process using ProcessBuilder. The main process waits until the external ...

20. Waiting for all threads spawned by my code under test in JUnit test case    stackoverflow.com

How do I ensure in a JUnit test case, that all the the threads spawned directly/indirectly by the method under test are done with there job, so that I can assert ...

21. Why does java thread wait() work only with time limit in here?    stackoverflow.com

I am trying to get familiar with Java threads for the SCJP and I had a question. In the below-written code i simply created: two Runnables with a common data storage (an ...

22. How to make the current thread wait for a function to return in java?    stackoverflow.com

I have a function in which the processing is given over to a new thread. The problem is the place where the function is called from does not wait for the function ...

23. Multithreading or observer pattern when waiting for dns lookup?    stackoverflow.com

I'm designing a system in java which utilizes a dns lookup class. My question is, when calling the class's dnsLookup(), whether to do it in a new thread or use the ...

24. Implementation of Atomic wait in Java    stackoverflow.com

The following is a translation of an atomic wait in Java. Here what is the need for while? Isn't if sufficient?

///<await (condition) statements; > 

synchronized(obj)
{ 
      ...

25. How to wait for all threads (variable number of threads) to finish to move on with Main?    stackoverflow.com

I will make many threads according to user input in a for loop. Therefore I won't be able to assign names for them. Is there a way to wait all of ...

26. Wait for exactly n works done: Producer, consumer and counter    stackoverflow.com

The problem i face is i have a thread A and n works need to be done. Thread A must wait until this n works are completely done. My idea is ...

27. In java, how to wait on multiple `Conditions` until any one of them is signaled    stackoverflow.com

Suppose an elevator simulation program, visitors about to take a ride are to wait until any one of the elevator doors opens. i.e. I want to wait on multiple Conditions until ...

28. Static webserver: Shortest possible wait: Low activity: Node JS vs bare bones Java thread based    stackoverflow.com

I am grasping Node JS after already knowing Java. Given the task to write a webserver that serves one page load every second. These are static pages. However, sometimes the page load ...

29. Java notfiyAll() not waking main thread    stackoverflow.com

I am trying to get notifyAll() and wait() to work for another piece of code. So I created a new program that behaves similarly to the other program. My program has my ...

30. Wait on multiple Conditions in Java    stackoverflow.com

I want a thread to wait until any of two Conditions are signaled. Is it possible? Suppose the thread is handling incoming packets of data, while it's possible a user interrupts ...

31. Threading - why do we do use while(true) while waiting    stackoverflow.com

In most of the threading examples in Java, there is common use of a while(true) block like this:

while(true) {
 try {
  wait() 
 } catch (Exception ex) {
   ...

32. Have a Thread wait for a Condition    stackoverflow.com

In a GUI, I have a few buttons. These buttons spin off worker threads that send requests over the network to a server. In a seperate thread, there is ...

33. Java - implementing a busy wait mechanism    stackoverflow.com

in my project I have until now "synchronized" multiple threads (each running the same type of Runnable) using a CyclicBarrier. In my case, using a CyclicBarrier turned out to ...

34. Threads wait until all threads return output using java?    stackoverflow.com

Possible Duplicate:
How to wait for a set of threads to complete?
I want to run three threads, each thread calls different classes. These classes do ...

35. Getting a Thread to wait indefinitely    stackoverflow.com

I have a Java Thread which handles outgoing communication with a Socket. I only want the thread to run while there is pending output ready to be sent. Say I have ...

36. What is the correct way to stop a thread waiting for network activity?    stackoverflow.com

This question has no doubt been asked in various forms in the past, but not so much for a specific scenario. What is the most correct way to stop a Thread that ...

37. Understanding java.lang.Thread.State: WAITING (parking)    stackoverflow.com

First, a really dumb question, I was just wondering what the waiting 'parking' means ? Is the thread waiting to be parked or is it just been parked and therefore is ...

38. How to determine the remaining wait time of thread in Java    stackoverflow.com

I understand that there is no specific method in the Thread class that allows a program to check how many remaining time a thread has before it wakes up. But in ...

39. Java Threads waiting value    stackoverflow.com

I have the following situation: In order to run a algorithm, i must run several threads and each thread will set a instance variable x, right before it dies. The problem is ...

40. How to wake up "waiting" thread?    stackoverflow.com

I am making an app that is intended to emulate how packets are transmitted over the internet, and so some packets get lost. The main thread waits for the response, however there ...

41. Java list of waiting threads    stackoverflow.com

Is there a way to get a list of waiting threads/number of waiting threads on an object?

42. wait until all threads finish their work in java    stackoverflow.com

I'm writing an application that has 5 threads that get some information from web simultaneously and fill 5 different fields in a buffer class.
I need to validate buffer data and store ...

43. Java: How to distinguish between spurious wakeup and timeout in wait()    stackoverflow.com

Here is a case where a thread is waiting for notify() or a timeout. Here a while loop is added to handle spurious wake up.

boolean dosleep = true;
while (dosleep){
  ...

44. wait()    coderanch.com

In your example the wait() method releases lock on the AThread object. Look at this example : public class MyThread extends Thread { boolean isRunnable = true; public MyThread() { start(); } public synchronized void run() { System.out.println("In run method"); while ( isRunnable ) { try { wait(); } catch ( InterruptedException ie ) { } } } public static void ...

45. wait(long timeout)    coderanch.com

Hey, I'm not sure of any technical way to differentiate between the two, but you could always just use a timer thread that starts just before you call the wait (long). Check the interval passed in the timer thread after the wait returns against the interval passed to the wait... Or you could capture the time before the wait and the ...

46. Waiting Thread    coderanch.com

JiaPei, Good question. I haven't seen one this good in quite some time, so bear with me while I ramble. I can think of two circumstances in which a thread contends for an object's lock. (I say "contend for" rather than "wait for" because it's helpful to differentiate between threads actively trying to get a lock and those in a wait ...

47. wait()    coderanch.com

Hi, This is from the JLS: The method wait should be called for an object only when the current thread (call it T) has already locked the object's lock. Suppose that thread T has in fact performed N lock actions that have not been matched by unlock actions. The wait method then adds the current thread to the wait set for ...

48. Behavior of wait()    coderanch.com

Could someone confirm (or correct) my understanding of the behavior of wait()? Assume we have three threads, call them A, B, and C, all using a single object, Obj, to synchronize. Assume that threads A and B call Obj.wait() from within code synchronized on Obj. They are now in a wait state on Obj. Thread C then calls Obj.notifyAll() and exits ...

49. Which thread comes first from wait().    coderanch.com

50. object wait(timeout) not timing out    coderanch.com

I have a main thread that creates an object with a Runnable interface, creates a thread out of it and then starts it. The main thread then waits for 180 seconds on the object. If after the timeout period, the object has not done its work, I want to stop the old object, create a new one and try again. Unfortunately ...

51. API for wait question    coderanch.com

If a thread becames "notified" then API states The thread T is then removed from the wait set for this object and re-enabled for thread scheduling. It then competes in the usual manner with other threads for the right to synchronize on the object; once it has gained control of the object, all its synchronization claims on the object are restored ...

52. How to make a thread wait and restart?    coderanch.com

Hi folks, my requirement is like from main thread i will start new thread ,Once the new starts the main thread has to be in wait state..and once the new thread stops i have to start the old one again How to go about it.. The following is where i require.. 1)From the main program i will call a Jdialog window.Now ...

53. two threads waiting for one resource    coderanch.com

hai everybody..i am new to this forum here's my problom if two threads are waiting for one resource or data,if the data came,which thread will get that data?.how can i make the 'data' availeable for only one thread?. or andy and freddy is waiting for alice.if alice came,who'll get alice?.Or how can i assign alice to freddy?. anybody plz wishing everybody ...

54. Barrier implementation: endlessly wait for what?    coderanch.com

Here is a simple class implementing barrier: class Barrier{ int nThreads = 0; int nBlockedThreads = 0; Barrier(int nThreads){ this nThreads = nthreads; } synchronized void sync() { nBlockedThreads++; if(nBlockedThreads < nThreads) { while(true) { try{ wait(); break; } catch(Exception e) {} } else { nBlockedThreads = 0; notifyAll(); } } } I know the concept of a barrier, but dont ...

55. Thread wait    coderanch.com

I found a thread issues notify didnt invoke the waiting thread immedeatly, instead the same thread which issues notify keep on running until it executes few statements. I thougth once a notify is executed it should immedeatly transfers control to any waiting thread.I think each thread is alloted with a paricular time slice, until that time slice is over thread will ...

56. Wait() doubt    coderanch.com

If you really want to wait for an interrupt and never are going to notify the thread, I think you'd best use sleep(long) instead (with a very long sleep time). The reason for suggesting this is simply that anyone reading your code, upon seeing wait(), will automatically assume there is a notify() somewhere. However, I'd want to query whether interrupt() is ...

58. Waiting for something to happen    coderanch.com

The code below defines an class that extracts various details from a response from a server. We then subscribe to a message server, on a specified topic, specifiy that the responses should be processed by the an instance of the class we just defined. The message sending is asynchronous. I want to wait after I have sent the request, till when ...

60. Wait()... do I understand it right?    coderanch.com

Ok... So.. I am studying Threading.. so... it is not the simpliest java subject. I want to make sure I understand the wait() method in the java.lang.Object class. For me.. wait() is very similar to join(). The difference is that when you join another Thread that Thread that joined will stop being at the runnable state until the Thread it joines ...

62. wait in while loop?    coderanch.com

Originally posted by Vasim Patel: Hi, Apologies for some basic questions. 1. Why is it necessary to put a wait() in a while loop. What happens if we don't. Is it a good programming practise or is it a MUST for wait() to function properly? 2. Also, when is an InterrupedException thrown? Can you anyone give me me a code snippet ...

63. How to wait until all threads finishes task?    coderanch.com

Hi all, I am in process of writing a java multithread program. Following are the steps. 1. In a method I am creating 4 threads public void test() { DocThread t1 = new DocThread("one"); t1.start(); DocThread t2 = new DocThread("one"); t2.start(); DocThread t3 = new DocThread("one"); t3.start(); DocThread t4 = new DocThread("one"); t4.start(); Thread currentThread = Thread..currentThread(); try { currentThread.sleep(10000); }catch(Exception ...

64. Is There Such Thing As An Implicit Wait?    coderanch.com

Well, I suppose it depends what you mean by "informed". If another thread has the lock on myObject, this thread won't be able to enter the synchronized block at first. So this thread will block, and it will not execute until the other thread exits its synhronized block and this thread acquires the lock. Behind the scenes, the JVM is managing ...

65. making a thread wait    coderanch.com

67. wait()    coderanch.com

68. object.wait()    coderanch.com

Originally posted by Nikos kat: How do you use the object.wait() method. if you put a number in parenthesis is it the wait in milliseconds? Is this as good as using Thread.wait() wait() is a method used by threads as a form of communication -- notifications to be exact. Threads calling wait() will be put into a block state until other ...

69. Exception on calling wait() method of Object class(WHY)    coderanch.com

You can only legally call wait(), notify(), or notifyAll() on an object if you hold the lock on that object -- i.e., inside a synchronized method called on that object, or a synchronized block locked on that object. If you have no idea what I'm talking about, then you probably want to call the static "sleep" method of the Thread class, ...

70. wait method calls    coderanch.com

71. can we access an instace variable of a thread which is in wait() mode?    coderanch.com

i am having a class R implementing runnabale interface. it has an instance variable A with its getter and setter methods. i am creating a thread T using interface R. In the run() method of R i am creating a different thread T1 using different Runnable interface R1 Thread T will be in wait mode until thread t1 has completed. In ...

72. wait() causing problems    coderanch.com

The main cause of your problem is the usage of the notifyAll() method. If there are more than one thread waiting, then you wake them all up. So if you put one object in the queue, the first one the wake up will take it while the others will get the error. However, even if you change it to notify(), this ...

73. question on wait()    coderanch.com

wait() doesn't wait for the lock; it actually releases the lock. What it's doing is waiting for some other thread to call notify() on that same locked object. This wait() is one-half of the inter-thread communications mechanism. Before wait() returns, the calling thread re-acquires the lock. Note that this is bad code -- wait() should always be called in a loop, ...

74. wait() help    coderanch.com

Hello all, I'm hoping you can help me, I have a class inplementing the main() method that does the basic work (running the motors of my robot) I have a thread that is constantly monitoring the sensors. when a sensor is activated a recovery method is called. problem is everything works great except I cannot get the main program to wait ...

75. Innterupted before waiting    coderanch.com

I have something that might fit that description. It starts a worker thread to do a couple long-running operations, but it only waits so long for it to finish. If it runs too long we interrupt it and go on my way. The worker thread never does any thread wait. // main thread doSomething() t = new thread( new runnable ) ...

76. How know thread.wait( timeout) has timed out?    coderanch.com

There's no way to tell directly - that is, you would have to add additional code to determine this. Often when you wait(), you're waiting for something to happen which changes the state of an object in some way - e.g. by setting a boolean variable, perhaps. If that's the case, then you may be able to simply check the state ...

77. Parent Thread should wait till all child thread are dead    coderanch.com

I have a Parent Class which spawns 10 child threads, class Parent{ public static void main(String[] args) { int threadWorkerNo = 10; processStartTime = System.currentTimeMillis(); spawnThreads(threadWorkerNo); System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"); } private static void spawnThreads(int number){ for(int i=1;i<=number; i++){ String name = Integer.toString(i); MigrationWorker th = new MigrationWorker(name); th.startThread(); } } } Assume the MigrationWorker is a Thread Class which does some work. now ...

78. Why notify/notifyAll() method doesnt invoke a waiting thread quickly?????    coderanch.com

I am currently working on a socket based IM server, I get messages from multiple clients simultaneously and put them into queues, for each queue in my server I have a separate listener running in a thread, as soon as a packet comes in the queue (from any client) its related thread is notified to get that packet from thread and ...

79. Waiting for all threads to stop    coderanch.com

Hi How do I ensure all threads finish executing before main() continues. I used something like this: for (int i : NUM_THREADS) { threads.start(); } for (int i : NUM_THREADS) { threads.join(); // line 1 } Does join() queue up the execution of threads ? For example, if threads 1 and 2 are running, main will wait at line 1 for ...

80. How to make a main thread wait until all other threads die out?    coderanch.com

In my program i have 10 threads working simultaneously, the threads are used to fetch data(one row per thread)from the database and send it to some other method. when the data in the database reaches the nth record the first thread that encounters this will spawn a main governing thread and it dies out. My problem is that i have to ...

81. wait+ interruptedException    coderanch.com

Thank you, I will give one scenario . class SharedResource { public synchronized void gotoWaitState() { try { wait(); } catch(InterruptedException e) { // here my doubt // } } public synchronized void comeFromWaitState() { notify(); } } class WaitStateThread implements Runnable { SharedResource ob2; public WaitStateThread (SharedResource x) { ob2=x; } public void run() { ob2.gotoWaitState(); } } class ReleaseThread ...

82. Is this a correct use of wait() ?    coderanch.com

Looking at the source of Nokia's sheepdog j2me sample, I noticed this snippet: public void run() { Thread currentThread = Thread.currentThread(); try { // This ends when animationThread is set to null, or when // it is subsequently set to a new thread; either way, the // current thread should terminate while (currentThread == animationThread) { long startTime = System.currentTimeMillis(); // ...

83. What exactly happens in a call to Object.wait()    coderanch.com

Hi ranchers, This question keeps boggling me as there's no explicit mention of what exactly happens at the OS level when a thread is made to 'wait' by a call to Object.wait() in the JDK documentation. I understand that when wait() is called, the current thread relinquishes control of the lock on the monitor of the object on which the wait() ...

84. Wait() Example    coderanch.com

85. How can a thread wait on itself?    coderanch.com

Hello, I'm not sure how this can be done. This is what I want to do: public static void main(String[] args) { Thread T = new Thread_extended_class(); T.start(); T.join(); int user_input_value = value; } class Thread_extended_class extends Thread implements ActionListener { public void run() { //present a GUI for the user to input 'value'; T.wait(); } public void actionPerformed(ActionEvent ev) { ...

86. Threads waiting in pool?    coderanch.com

I'm using an MDB driven architecture to take messages from a queue and persist them in a database. For this, I've configured a pool of Listener Ports to take messages from the queue. When I run a stress test and take a core dump, I see a number of threads waiting at the same location (see stack trace below). What does ...

87. Count of waiting threads    coderanch.com

I think there's no way to know ,from within a Java program,the size of the wait set (the set containing the threads waiting on the mutex) of an object's monitor when the synchronized block is used,the reason being the fact that the implementation of the synchronization logic,achieved through the synchronized keyword,is internal to the JVM. However it would have been good ...

88. wait() question from kb book    coderanch.com

Hi all, Am gonna tell what I understood from the following code. Can anyone tell me if am wrong? 1).At any point of time, main thread and thread b can be in their respective synchronized blocks at the same time (coz even though both the threads have their lock on object b, both are methods of different classes). 2).once the main ...

89. Thread.wait() doubts.......    coderanch.com

I have a thread application which waits for a signal from a third party application and based on the notification from it, my thread will come out of wait and continue it's processing. I am running into a situation where, the thrid party application is not sending the notification to my thread, because of which my threads keeps on waiting and ...

90. how to set timer for wait() call    coderanch.com

92. Thread waiting for another thread    coderanch.com

Hi All, I am using an API to get some streaming data from a server. It has self-explanatory classes like DataClient, Session, Connection, etc. The implementation class implements DataClient which should initiate Session and Connection objects. When required calls are in a main method, it would look like this DataClient client = new DataClient(); // initialize lister client Session.initialize(); // initialize ...

93. Wait Thread ??    coderanch.com

Hi all, I have a servlet which loads some data to the database from a text file & even executes some other queries on the loaded data. But as the data as increased, the connection to the server expires, how do I go around this problem, I thought of running some thread during the process which say "Your DATA is being ...

94. Waiting for input from PipedInputStream....thread yield?    coderanch.com

Hello All, I've written a test class to start playing around with PipedInputStreams and PipedOutputStreams. I need to work the kinks out of the functionality before I integrate it into my real app. Essentially what I've done is created 2 threads -- one has a PipedOutputStream to which it writes messages, the other thread has a PipedInputStream from which it reads ...

95. Waiting for all threads to die    coderanch.com

Hi How do I ensure all threads finish executing before main() continues. I used something like this: for (int i : NUM_THREADS) { threads.start(); } for (int i : NUM_THREADS) { threads.join(); // line 1 } Does join() queue up the execution of threads ? For example, if threads 1 and 2 are running, main will wait at line 1 for ...

96. Calling methods on a waiting thread    coderanch.com

97. How to wait in main method until all thread are finished    coderanch.com

I am very new with thread. Here is my problem. I have one JSP where myApp.main method called (something like below): ... myApp.main(args); anotherObject.doSomething(); ... Separate threads creates from after we call test.start(); .Right now the main method return where it was called from (my JSP), before all the thread finished (created from test.start()) That means it ...

98. Thread Wait()    coderanch.com

The following code produces the output as below when the line BBBB is commented and the line AAAA is uncommented: public class ThreadA { public static void main(String[] args) { try { ThreadB b=new ThreadB(); b.start(); b.wait(); /* [b]Line AAAA[/b] */ b.game(b); } catch(Exception hj) { } } } class ThreadB extends Thread { public void run() { System.out.println("52 "+Thread.currentThread().getName()); this.game(this); ...

99. Wait method (multithreading) currentmethod    coderanch.com

Current Thread is a static method in Thread class. It returns the Thread name,priority (by default 5 i.e NORM_PRIORITY) and method name in which it was invoked. i.e if you call method currentThread in run mthod of Thread and then start thread from the main method, answer would be like Thread[Thread-0,5,main]. And about wait() method is not the static method.It is ...

100. wait() is not resuming    coderanch.com

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.