I am experimenting with Multithreading in java, basically I want to make two threads, and have them count to a number.
But when they reach about half way they sleep for a set amount of time. So I was thinking to put an IF statement within the FOR loop to see when it reached halfway and then if it has, it will put itself to sleep.
I am running a single core cpu atm because I am on a school computer so in theroy, when I put the first thread to sleep, the second one should start?
Also, is it possible to put the first thread to sleep, from the second one and vice versa?
You can use the
Thread.sleep(int milliseconds):As for making one thread pausing another, you could expose methods which the other threads can call to pause the execution of other threads.
Just like any other multi-threaded program, keep an eye out for race conditions.
As for your other question, single core processors should be able to run multiple threads, so the amount of cores (in this case) does not really matter.