JAVA
java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Created by Minho 🇰🇷
// Modified by Elizabeth Kelly for experimentation purposes
/*
While thread priorities are supposed to manage threads as described, the scheduling is not guaranteed.
Thread scheduling varies by OS, the amount of CPU available, the resources the thread requires and several other factors.
A thread context switch is also resource intensive.
I/O (ie...System.out.println) operations and other operations that are synchronous can cause a block, allowing the other thread a chance to execute.
Altogether this can create unpredicable results when using thread priorities alone.
If the threads are independent, then the order of their execution should not matter.
But if they need to access a shared resource, then thread synchronization techniques must be used to ensure the exact sequencing desired.
If they aren't using a shared resource and synchronization is required then threads should not be used (that defeats the whole purpose of using threads).
This code captures a time stamp with each pass of a for loop, then prints the results. This eliminates potential
problems due to use of I/O within the thread.
The results trend is that the first iteration of the for loop in the name thread is able to complete, then the context switch occurs allowing the welcome thread to complete all 3 passes of its for loop. The name thread then resumes.
But...these results (sorry they are hard to read) are also inconsistent which leads me to believe this variation is caused by how SoloLearn is managing the CPU allocation and/or network performance.
Incidentally when I run this on my local machine the priorities are completely ignored. Apparently that is known Windows behavior (and maybe that is why we never did any assignment that required thread priorities at my school?).
*/
import java.util.*;
import java.time.*;
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run