Thread_1 and Thread_2 never begin to execute until after Thread_3 is finished, even though Thread_3 is started last.
Here's the code:
class MyThread extends Thread
{
public MyThread(String name)
{
super(name);
}
@Override
public void run()
{
for (int i = 0; i < 10; i++)
System.out.println(getName() + " " + i);
}
}
public class ThreadTest
{
public static void main(String[] args)
{
Runnable runnable = () ->
{
for (int i = 0; i < 10; i++)
System.out.println("Thread_3 " + i);
};
MyThread thread_1 = new MyThread("Thread_1");
MyThread thread_2 = new MyThread("Thread_2");
Thread thread_3 = new Thread(runnable);
System.out.println("Thread_1 Priority " + thread_1.getPriority());
System.out.println("Thread_2 Priority " + thread_2.getPriority());
System.out.println("Thread_3 Priority " + thread_3.getPriority());
thread_1.start();
thread_2.start();
thread_3.start();
}
}
One of the results:
Thread_1 Priority 5
Thread_2 Priority 5
Thread_3 Priority 5
Thread_3 0
Thread_3 1
Thread_3 2
Thread_3 3
Thread_3 4
Thread_3 5
Thread_3 6
Thread_3 7
Thread_3 8
Thread_3 9
Thread_2 0
Thread_1 0
Thread_2 1
Thread_1 1
Thread_1 2
Thread_2 2
...
Whatever I try, thread_1 and thread_2 never begin to execute until after thread_3 is finished.
OS: Windows 10, JDK Version: 16.0.2