This discussion is archived
2 Replies Latest reply: Feb 23, 2013 10:15 AM by 668085 RSS

Green threads in Java?

668085 Newbie
Currently Being Moderated
I have a situation where I would like to create green threads rather than native threads. Basically, I want to exploit a thread's ability to maintain a separate stack and code pointer but not have it run concurrently.

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. I could then call the run() method again, and my green thread would run until it blocks or yields again. So everything would really be on a single thread - I'd just have multiple green threads independently keeping track of stack and code pointer info with my main thread constantly switching between them.
  • 1. Re: Green threads in Java?
    baftos Expert
    Currently Being Moderated
    kitfox wrote:
    Ideally, I would do something like create a Thread object in my main thread and then call run() on it.
    At this point you created a Thread object, not a thread.
    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
  • 2. Re: Green threads in Java?
    668085 Newbie
    Currently Being Moderated
    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:
    http://www.java-gaming.org/topics/java-continuations-and-greenthreads/28337/view.html

    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.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points