Computer Science Canada

Threads

Author:  person [ Sat Jun 02, 2007 4:08 pm ]
Post subject:  Threads

I'm using a thread in my program. And I'm running into problems making the threads sleep.

I have one class that contains the thread (call this class Class1) , and another class that contains other codes (it also calls the thread) (call this Class2).

I want the class that calls the thread to be running at full speed, while the thread being called pauses a set amount of milliseconds before running again. From the API, I have found that the command Thread.sleep (int ms); will make the thread sleep when I call it. However, if I place it in the class that is the thread (Class1), nothing happens and it doesn't sleep. If I put it in the class that calls the thread (Class2), everything slows down, not just the Thread.

Would there be anyway to just slow the thread down and not Class2?
Thanks.

Author:  rizzix [ Mon Jun 11, 2007 4:19 pm ]
Post subject:  Re: Threads

Concurrency usually makes things very complicated. If I assume that the reason you want a certain thread to sleep is because you want other threads to operate at full capacity, then I think what you want to do is to "synchronize" the threads. Any thread can enter a synchronous lock on a given object, it can then "wait" till it gets "notified" to continue. While in a wait state, the thread literally stops. Thus I believe most of the processing power will be transferred to the other thread.

I'm not well versed with concurrency, but you can look up, wait and notify (in java Threads) and you'll get some idea, hopefully.


: