1. Thread Priority
The maximum priority is 10, the minimum value is 1, the default is 5, the priority of the thread is set, not exceeding its range of values, or it throws an exception.
High priority threads will give priority to the control of the Cup's time slice.
Rabbitthread rabbitthread = new Rabbitthread ();
Rabbitthread.start ();
Rabbitthread.setpriority (thread.min_priority);
The garbage collector's thread has a very low priority, so even if the GC method is invoked, it is not always possible to get immediate execution.
2.join (Connect, join, join)
If another thread B is started in one thread A, and the Join method of the B thread is invoked. Then a thread waits for the B thread to finish executing before continuing.
Chengthread.start ()/Ready
Chengthread.join ();
3.sleep (Hibernate)
Thread.Sleep (Long ms)
Thread hibernate, specifying when the thread sleeps. The parameter is milliseconds.
The current thread will enter a blocking state if the sleep method is executed. and transfer the control of the CPU. Only wait until the time of hibernation is over. Then enter the ready state. Wait for the CPU to dispatch.
. Sleep (time);
DateFormat SDF = new SimpleDateFormat ("HH:mm:ss");
By this method, the execution body of a thread can be well controlled and the time period of single execution.
4.yield (yielding)
Thread.yield () makes the current thread tentative, enters the ready state, and gives up control of the CPU. Let the other thread execute.
5. Daemon Threads
Daemon thread background thread must be set before Start (Start, Start) method, and once the front end thread is terminated, all daemons will be terminated.
Thread1 thread1 = new Thread1 ();
Thread1.setdaemon (True); (Set daemon)
Thread1.start ();
6. Thread sync synchronized (sync)
Obj in synchronized (obj) {} is called Synchronization monitor
Synchronization monitor can be any object in a synchronized code block, but it is recommended that you use a shared resource as the synchronization monitor
Synchronization monitor does not need to be specified in the synchronization method because the monitor for the synchronization method is this, which is the object itself
Synchronizing the monitor's execution process
The first thread accesses, locks the synchronization monitor, executes the code
The second thread accesses and discovers that the sync monitor is locked and cannot be accessed
The first thread has been accessed and the Sync Monitor is unlocked
The second thread accesses, discovers that the sync monitor is not locked, locks and accesses