How to make simultaneously executed threads?

I want two separate threads to execute two different instances of different classes, and I want them to execute the launch command at the same time.

I did a practice class to demonstrate the problem I am having. One rider counts forward, the other counts back.

public class testCount { public static void main(String args[]) { testCount countCompetition = new testCount(); countCompetition.run(); } public void run() { (new Thread(new racer1())).start(); (new Thread(new racer2())).start(); } public class racer1 implements Runnable { public void run() { for(int x = 0; x < 100; x++) { System.out.println(x); } } } public class racer2 implements Runnable { public void run() { for(int y = 100; y > 0; y--) { System.out.println(y); } } } } 

My results

 1 2 ... All the way to 100 100 100 99 ... All the way back down 1 

What I want

 1 100 2 99 3 98 

They do not need to do this, but they must work at the same time, and not one after another. Any hints, tips, or code snippets would be appreciated.

+4
source share
3 answers

Just add Thread.sleep(1); to each racer class after System.out.println() .

i.e. it will look like this:

 public class racer1 implements Runnable { public void run() { for(int x = 0; x < 100; x++) { System.out.println(x); try { Thread.sleep(1); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } } 
+1
source

I think that all the answers still do not make sense.

Your existing logic allows your two threads to execute simultaneously at the same time, but this is not obvious, because your numbers increase to 100, and the execution usually stays with a certain thread for more than one command at a time, otherwise when switching between the current executable thread it will be all the time too a lot of overhead. In your case, the JVM decides to run its first thread long enough so that it can print 100 numbers before "switching context" to the second thread. The JVM can choose to execute threads differently, so the result you see is not guaranteed to be the same every time.

If you increase your numbers even to 1000, you are likely to see several threads, several interleaved. You will still have large runs where one thread will produce many numbers in a row, because it is more efficient for the JVM to execute one thread for a while before switching, instead of switching the context between each instruction.

Adding Thread.sleep (1) is not a good solution when adding unnecessary delay. Of course, for 100 numbers this may not be noticeable, but for 10,000 numbers you will have a delay of 10 seconds.

Is there any reason why you need them to alternate them to a higher degree than they already do? If so, then your simple model of running two threads simultaneously is not enough. If not, just let the JVM choose the best order to start your threads (which in the simple example that you specified means that they probably won't interleave in most cases).

+6
source

You need to write a basic waiting and notification system. One task is to notify the other that he was catching work. The basic idea can be taken from the code below. create 2 tasks, one for counting forward and one for counting back

 Runnable task = new Runnable() { public void run() { System.out.println("woohooTwo"); synchronized (t) { while (true) { System.out.println("---" + Thread.currentThread().getName() + "--" + tigetAndIncrement()); t.notifyAll(); try { Thread.sleep(1000); t.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }; 
-1
source

All Articles