User thread:
When a thread is not finished running, the JVM does not actively shut down the thread. This thread is called the user thread
That is: The JVM does not actively shut down as long as the user thread is not running (the thread defaults to the user thread)
Main method:
public class Main {public
static void Main (string[] args) {
userthread ut = new Userthread ();
}
}
Thread 1:
public class Userthread extends thread {public
userthread () {
start ();//After the thread object is created, the thread automatically enters the ready queue
}
public void Run () {for
(;;) {
System.out.println ("==================================");}}}
Daemon Thread:
When the JVM discovers that only the daemon is running, the JVM actively shuts down the daemon thread and shuts down the JVM
That is, the JVM does not keep the daemon running.
Main method:
public class Main {public
static void Main (string[] args) {
//After the thread object is created, the default is the user thread
daemonthread dt = new Daem Onthread ();
Mark the user thread as the daemon thread
Dt.setdaemon (true);
Dt.start ();
try {
thread.sleep (1000);//If not sleep () will be directly closed not output
} catch (Interruptedexception e) {
//TODO auto-generated Catch block
e.printstacktrace ();}}}
Thread 1:
public class Daemonthread extends Thread {public
daemonthread () {
} public
void Run () {for
(int i = 1; I <= 10000000; i++) {
System.out.println ("==================================i=" + i);}}}
Thread's Join () method:
//We call the thread's join () method in the main threads. The main thread is blocked. Until the T1 thread finishes running
Main thread unblocking state, back to ready queue
In other words: As long as the thread is not running, the main threads will never execute
Main method:
public class Mainthread {
private static string[] arr = new string[50000];
public static void Main (string[] args) {
//We first create the thread of the initial array, and the array element can be printed in the main thread only after the thread that initializes the array has finished initializing the array
initthread t1 = New Initthread (arr);
try {
//We call the Join () method of the T1 thread in the main thread. The main thread will be blocked. Until the T1 thread finishes running
///The main thread is unblocked, back to the Ready queue
//In other words: As long as the T1 thread is not running, The main thread will never execute
t1.join ();
} catch (Interruptedexception e) {
e.printstacktrace ();
}
Print array elements in the main thread for
(int i = 0; i < arr.length; i++) {
System.out.println ("Print array elements in main thread: arr[" + i + "]=" + arr[ I]);}}}
Thread 1:
Initializes the array of threads public
class Initthread extends thread {
private string[] arr;
Public Initthread (string[] arr) {
This.arr = arr;
Start ();
}
public void Run () {for
(int i = 0; i < arr.length; i++) {
String str = "hello-" + i;
Arr[i] = str;
SYSTEM.ERR.PRINTLN ("Initialize thread at initialization of array element: arr[" + i + "]=" + Arr[i]);}}}
hope to help you, wish you have a good mood, refueling.
If there are errors, incomplete, can be optimized points, welcome to correct and supplement; reprint please indicate the source.