I made a concurrency class to test out threads. since I wanted to find out the best way to run threads at the same time.
I am surprised by my results:
test
test
Othertest
test
Othertest
test
test
test
The results I expected were for the threads to come back randomly yet they seem to come back consistently in the same order! Does anyone know why? Does this mean that they are not running concurrently? How might I go about getting them to run at the same time?
Here is my code:
public class ThreadTest {
public static void main(String args[]) throws InterruptedException
{
new Thread(new ThreadTest().test()).start();
new Thread(new ThreadTest().test()).start();
new Thread(new ThreadTest().otherTest()).start();
new Thread(new ThreadTest().test()).start();
new Thread(new ThreadTest().otherTest()).start();
new Thread(new ThreadTest().test()).start();
new Thread(new ThreadTest().test()).start();
new Thread(new ThreadTest().test()).start();
}
public Runnable test() throws InterruptedException{
Thread.sleep((long) (Math.random()*1000));
System.out.println("test");
return null;
}
public Runnable otherTest() throws InterruptedException{
Thread.sleep((long) (Math.random()*1000));
System.out.println("Othertest");
return null;
}
}