This content has been marked as final. Show 2 replies
kitfox wrote:At this point you created a Thread object, not a thread.
Ideally, I would do something like create a Thread object in my main thread and then call run() on it.
The thread would then run until it hit a sleep() or yield(), at which point control would return to my main thread."The thread" does not exist. You are still in the main thread, executing the Thread object's run() method. Therefore, when you hit sleep(), your main thread will sleep and will not be able to do anything for as long as it sleeps.
Yeld() requests the VM to let other threads than the current one execute, if any. This agin will not help at all, as your current one is the main thread and there are no other threads.
Java creates new threads when a Thread object's start() method is called.
Maybe you can explain what is the situation that you have and someone may come with better ideas.
Edit: After reading your other posts, I would guess that you don't quite know about the ability of threads to control each other's advance via notify() and wait(), which are methods of the Object class. Note that when I say "thread" (lowercase t) in this post, I am very careful with terminology as not to confuse it with "Thread object" or "Thread class".
Edited by: baftos on Feb 23, 2013 12:52 PM
Yes, I know what I described was not actually a thread. I was using it as an analogy.
I'm looking for something like what's described here:
However, I'm a little nervous about using something that rewrites byte code and was hoping there might be a more official way of doing it. I hadn't considered notify() and wait() before, and they sound similar to what I'm looking for. However, it also looks like I have to create a separate native thread for each of them and I have no control over when their awoken thread is scheduled to run. The green thread library described in the article lets me create a monitor object, hand my green thread to it, and then call a tick() command to run my green threads when I see fit.