Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Using Thread waiting() method

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Using Thread waiting() method

    I am trying to send a notify to an available Thread (first Thread i can find in a pool currently in a WAITING state), but am unable to. I am trying to send the notify alert to a thread running in a separate class, although this class is included in the same package. Here is the Server.java file from which I am trying to notify my available thread:


    ***************Server.java***********************
    package dir.test;
     
     
    import java.util.ArrayList;
    import java.util.LinkedList;
     
    public class Server {
    	static private final int THREADCOUNT=5;
     
     
    	public static void main(String[] args) throws InterruptedException {
    		ArrayList<Thread> objThreads = new ArrayList<Thread>(THREADCOUNT);
     
    		for (int i=0; i<THREADCOUNT; i++) {
    			objThreads.add(i, new Thread(new ObjThread(1)));
    			objThreads.get(i).start();
    		}
     
    		try {
    			for (int i=0; i<objThreads.size(); i++) {
    				if ( (objThreads.get(i).getState())==Thread.State.WAITING ) {
    					System.out.println("Thread " + i + " is in state: WAITING.");
    					objThreads.get(i).notify();
    					System.out.println("Thread " + i + " has been notified.");
    				}
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		} catch (Throwable t) {
    			System.err.println("Caught throwable t: " + t);
    		}
    	}
    }

    **********************************************

    and here's the ObjThread.java file...

    ***************ObjThread.java********************* **
    package dir.test;
     
    import java.lang.Runnable;
    import java.util.*;
     
     
    public class ObjThread implements Runnable {
    	private int int_var;
     
    	public ObjThread(int int_var) {
    		this.int_var = int_var;
    	}
     
    	public synchronized void run() {
    		while ( true ) {
    			try {
    				System.out.println("test...before wait");
    				wait();
    				System.out.println("test...after wait.  int variable value: " + int_var);
    			} catch ( InterruptedException e ) { }
    		}
    	}
    }

    **********************************************


    The thread never gets to the statement:
    System.out.println("test...after wait. int variable value: " + int_var);

    My output is:
    $ java dir.test.Server
    test...before wait
    test...before wait
    test...before wait
    test...before wait
    test...before wait
    Thread 0 is in state: WAITING.
    java.lang.IllegalMonitorStateException
    at java.lang.Object.notify(Native Method)
    at dir.test.Server.main(Server.java:23)


    Any idea how I can wake a thread from a WAITING state with this file structure so it can resume it's work?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,273
    Thanks
    177
    Thanked 822 Times in 765 Posts
    Blog Entries
    5

    Default Re: Using Thread waiting() method

    Acquire a lock on the objects...flank the wait and notify by a synchronized statement on the object you are calling prior to calling these methods.

Similar Threads

  1. Thread Hangs at split method.
    By kailasvilaskore in forum Threads
    Replies: 1
    Last Post: November 26th, 2010, 11:13 AM
  2. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM
  3. Waiting until condition
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: October 22nd, 2010, 09:24 AM
  4. Thread to EDT
    By Asido in forum Threads
    Replies: 3
    Last Post: October 15th, 2010, 01:13 PM
  5. Thread runs only once and not again
    By enflation in forum Threads
    Replies: 3
    Last Post: June 9th, 2010, 10:51 AM