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
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.
Re: How Can I run java threads
thanks but let me say my problem by an example:
suppose a C program like this:
Code :
//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.
Code :
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
Code :
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
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.
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
Code :
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
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?
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
Re: How Can I run java threads
Quote:
I want to run my thread(s) for ever
They will continue to execute if they don't return from the run() method.
Quote:
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?
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
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.
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
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.
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.