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 13 of 13

Thread: How Can I run java threads

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How Can I run java threads

    Hi All
    I have a thread. first I start it, then it executes the run() method automatically. this thread reads data from a linked list. if the list is empty then I use wait() to block the thread. when I insert an item to the list I use notify() to let waited threads to run. but my problem is that:
    1. I want to run this thread continuously
    2. I want to thread executes from any where that it waited. I mean from any line of code that wait stops it from executing.

    please help me how can I use java threading methods to run my threads continuously?

    with all my thanks: Saeid


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How Can I run java threads

    I'm not quite sure what it is you want, tell me if this is something like what you want:

    1. Have a thread poll a list for items that need to be processed. If there is something, process it. If there isn't, sleep for some time and re-start at step 1.

    Normally you don't use wait() unless you are waiting for an object lock. The preferred method would be to release the lock you own on the list and then call Thread.sleep().

    You can run a thread continuously by using an infinite loop. Be sure that you set the thread to be a daemon so that when your main application quits (all non-daemon threads have finished), the thread will automatically be stopped.

  3. #3
    Junior Member
    Join Date
    Aug 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How Can I run java threads

    thanks but let me say my problem by an example:

    suppose a C program like this:
    //something here
    scanf(...)
    scanf(...)
    //something else

    what happens when this program being executed?
    the programs waits until the first data has entered. after that it waits too until the second data has been entered. now I want to do that with threads.
    here is my code. the pop returns a data item from a linked list that is "InChannel"
    InputType is my generic class type.
    	public  InputType Pop()
    	{
    		synchronized(InChannel){
    		 if(InChannel.Size() ==0)
    			try
    		    {
    		       InChannel.wait();
    			}catch (Exception e){}
    		}
    		return(InChannel.Pop());
     
    	}

    now the code for inserting in the list that is "OutChannel"
    OutputType is my generic class type
        public  void Push(OutputType Data)
    	{
        	synchronized(OutChannel){
    		OutChannel.Push(Data);
    		OutChannel.notify();
        	}
    	}

    also I should say that InChannel and OutChannel, both are points to one linked list

    now what should I do? which methods of threading class and how should I use them?

    Please help me if it is possible.... with all my thanks

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How Can I run java threads

    hmm... so you're enqueueing items onto a linked list and reading them off from different threads?

    I would still go ahead with the suggestion I made:

    The enqueue thread would obtain the lock for the linked list (through synchronization), then enqueue the item at one end (say the tail). It will then release the lock for the list (very important).

    The dequeue thread would obtain the lock for the linked list, then check to see if anything is on the list. If there isn't, it must release the lock (otherwise the enqueue thread can never get it to put stuff onto the list), then use Thread.sleep() to sleep that thread for some time, allowing other threads to work. If there is an item, (while it still has the lock), dequeue the item, then release the lock.

    edit: for a practical example of this, see Swing Console Component, I created streams for handling input/output that uses synchronization. It will vary a little bit from what you have, but it uses the same locking/unlocking mechanism I described.
    Last edited by helloworld922; August 3rd, 2010 at 03:22 PM.

  5. #5
    Junior Member
    Join Date
    Aug 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How Can I run java threads

    Thanks for reply
    I examined sleep(...) but it was not worked like I want.you know, now I have got another message:
    Illegal ThreadStateException. Of course I know the code below is not right. but I don't know how to write correct code. my code is
    public class Main {
     
    	/**
    	 * @param args
    	 */
        static Pipe p=new Pipe();
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		while(true)
            {
    			p.start();
            	        p.run();
            }  
     
    	}
    }

    for this code, SOME TIMES, the first round gives correct answer but after that
    I get Illegal ThreadStateException.

    what should I do?

    Thnks: Saeid

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How Can I run java threads

    What do you want the code to do?

    What does the API doc say about calling start() and run() more than once?

  7. #7
    Junior Member
    Join Date
    Aug 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How Can I run java threads

    calling the start() method for one thread more than once is not allowed.
    but I want to run my thread(s) for ever. and I have a problem that I think once a thread goes to wait() state it never comes back to continue it's execution!
    Please read my last posts......
    with all my thanks:Saeid

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How Can I run java threads

    I want to run my thread(s) for ever
    They will continue to execute if they don't return from the run() method.

    problem is that:
    1. I want to run this thread continuously
    Which thread? The one that is waiting for more data in the linked list or the one that has just added to the list?
    Last edited by Norm; August 6th, 2010 at 12:56 PM.

  9. #9
    Junior Member
    Join Date
    Aug 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How Can I run java threads

    I have 2 threads. these threads shares a linked list between them. but one of them writes into list and the other one reads from that. now, what should be happen if the list is empty?
    the reader should waits until the writer writes an item into the list.
    I have posted the the read method called Pop() and the write method called the Push()
    in previous posts. please read them and help me what should I do.

    i am waiting for your replies: Saeid

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How Can I run java threads

    The wait and the notify should be on the same object. Your code shows different objects.

    Read the API doc for those methods.

  11. #11
    Junior Member
    Join Date
    Aug 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How Can I run java threads

    the InChannel and OutChannel points to one list.
    thread2.InChannel=Thread1.OutChannel.
    then if I use the InChannel.wait(), in fact it waits for the outChannel and when I use the OutChannle.Notify() it means the thread that waits for this channel should be waken up.
    but if you think that my senario is not true please say me how can I implement my idea?
    how can I have the same lock for both objects?

    thanks: Saeid

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How Can I run java threads

    wait and notify must be called on THE SAME EXACT OBJECT.
    To coordinate two threads using wait and notify, they must use one object.

    You'll need an object for each case where a thread has to wait for an event.

  13. #13
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How Can I run java threads

    I posted a link in my last post that has an example of piping input/output between two different threads (including waiting on the read thread if there's nothing to read). Please read that and you should be able to adapt it to use a linked list as you want.

Similar Threads

  1. Working with threads
    By tccool in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 12th, 2010, 10:21 AM
  2. [SOLVED] Questions About Threads
    By neo_2010 in forum Threads
    Replies: 4
    Last Post: March 15th, 2010, 09:04 AM
  3. threads in gui
    By urosz in forum AWT / Java Swing
    Replies: 1
    Last Post: November 3rd, 2009, 05:20 PM
  4. [SOLVED] Fixing of bug for a small program
    By Koren3 in forum Threads
    Replies: 3
    Last Post: April 21st, 2009, 06:28 AM