java-Multithreading-Thread control __java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.