Multithreading details and Multithreading

Source: Internet
Author: User

Multithreading details and Multithreading
1. Similarities and Differences between sleep and wait Methods

Sleep and wait both allow the thread to sleep

Different:

Sleep Needs to specify the time, while wait can specify the time or not.

When the sleep time is reached, the thread will return to the running state. If no time is specified for wait, you must use the Y and notifyAll methods to wake up wait.

Sleep can use synchronous code blocks, or do not use synchronous code blocks. wait must use synchronous code blocks.

Sleep does not release the lock after sleep, and wait releases the lock.

2. Stop the thread and interrupt method

Generally, the thread is finished by running the run method. If you want the thread to stop running in advance, you can use the stop method. However, the stop method is out of date due to insecure factors. The stop method provides the corresponding solution: some variables or tags can be defined in the thread, and then the variable or tag can be determined during the thread running, finally, the thread execution is still completed. If the thread remains in the waiting (frozen) state for a long time, you can use the interrupt method to interrupt the waiting.

Interrupt()In fact, it is to clear the waiting state of the thread and restore the thread to the running state again.

1 // test thread stop and interrupt wait 2 3 class Demo implements Runnable 4 {5 boolean flag = false; 6 public void run () 7 {8 while (true) 9 {10 synchronized (this) 11 {12 System. out. println (Thread. currentThread (). getName () + "...... "+ flag); 13 14 try {this. wait () ;}catch (Exception e) {}15 16 if (flag) 17 {18 break; 19} 20} 21} 22 23} 24} 25 class ThreadDemo 26 {27 public static void main (String [] args) 28 {29 Demo d = new Demo (); 30 31 Thread t = new Thread (d); 32 Thread t2 = new Thread (d); 33 34 t. start (); 35 t2.start (); 36 37 38 for (int I = 0; I <100; I ++) 39 {40 System. out. println (Thread. currentThread (). getName () + "========" + I); 41 if (I = 50) 42 {43 d. flag = true; 44 // clear the waiting state of Thread-0 45 t. interrupt (); 46 t2.interrupt (); 47} 48} 49} 50}
3. daemon thread, thread group, and thread priority

Daemon thread: Background thread. When the thread we directly create does not set any of its attributes, this thread generally belongs to a non-daemon thread (foreground thread ).

The non-daemon thread is executed together with the main thread. After the main thread is executed, if other non-daemon threads are not executed, the current program is still running normally.

Daemon thread: it is also used to execute thread tasks. It will be executed along with non-daemon threads. When no non-daemon thread is running in the program, no matter how many daemon threads there are, they are not executed in time, and the program will automatically stop running. The daemon thread automatically stops running.

 

Thread group: threads that have completed similar functions can be combined to manage the current threads by using a group. That is, threads can be managed by groups.

 

Thread priority: the thread priority is identified by numbers from 1 to 10. Generally, the priority of the current thread is marked with 1 5 10.

A thread with a higher priority executes more times than a thread with a lower priority. The priority is high. It only increases the chance of the cpu to execute this thread, rather than the cpu to execute it all the time.

GetPriority()Obtains the priority of the current thread.

SetPriority(int newPriority)  Sets the thread priority. The newPriority parameter can be 1-10! We recommend that you set the numbers 1, 5, and 10.

Daemon source code:

1 // test thread stop and interrupt wait 2 3 class Demo implements Runnable 4 {5 boolean flag = false; 6 public void run () 7 {8 int num = 1; 9 while (num <20) 10 {11 System. out. println (Thread. currentThread (). getName () + "...... "+ num ++); 12} 13} 14} 15 class ThreadDemo2 16 {17 public static void main (String [] args) throws InterruptedException18 {19 Demo d = new Demo (); 20 21 Thread t = new Thread (d); 22 Thread t2 = new Thread (d); 23 t. setPriority (10); 24 t2.setPriority (1); 25 26 // set both threads as the daemon thread 27 t. setDaemon (true); 28 t2.setDaemon (true); 29 t. start (); 30 t2.start (); 31 32 for (int I = 0; I <10; I ++) 33 {34 System. out. println (Thread. currentThread (). getName () + "========" + I); 35 // temporarily pause the main Thread for 36 threads. yield (); 37} 38 39 System. out. println ("......................... "+ t); 40} 41}
4. Introduction to join and yield methods

Join()  Add a thread to the execution queue of the current cpu. When join is used to join other threads to the running state, the join statement must be written in the Execution Code of other threads. The thread where the code to be written to the thread is located will process the temporary freeze state. After the execution of the added thread ends, it will immediately resume the running state.

Yield()  Pause the currently executing temporary pause and immediately resume running.

 

Technologies to be mastered for multithreading:

1. Two ways to create a Thread inherit the Thread and implement the Runnable interface

2. Master the cause and solution of thread security issues

3. Master the single production and single consumption principles and the wait for wake-up Mechanism

5. Interview Questions

  

  

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.