Java multi-Threading in-depth analysis

Source: Internet
Author: User

1. The difference between a process and a thread

1.1 process usually refers to an application, the start of the process means that the main thread of the program starts, different processes have this different memory space, and different threads have shared memory space, a process contains multiple threads

How to create a thread in 2.java

2.1 Inheriting the Thread class

2.2 Implementing the Runnable interface

3. The difference between run () and start () in a thread

The 3.1 Run method refers to what a thread is going to do

The 3.2 Start method refers to turning the state of the thread into a ready state, and whether the thread execution is dependent on the CPU's dispatch

4. Scheduling in Threads

4.1 Sleep method: How long the thread sleeps

4.2 Join method: The thread waiting for the join method becomes extinct

4.3 SetPriority method: Sets the priority of the thread, the greater the value, the greater the resource for the CPU scheduler

4.4 Yeild Method: Thread concessions, so that the current thread into a ready state, although it is a concession but it is likely to execute immediately

4.5 Stop Method: Forces the current thread to stop, but it is unsafe, so the JDK is marked as obsolete

4.6 Interrupt Method: Gracefully Stop the thread (set a signal that can stop the thread, and actually stop or not) should look at the business results that the thread executes can be isinterrupt to determine if the current thread can stop.

5. Status of the thread

    

6. Parallel to concurrency

6.1 Parallel execution in real sense at the same time

6.2 With CPU scheduling, only one thread executes at the same time

7. Problems associated with concurrency

Multi-threaded, atom, visible, orderly, detailed explanation:

7.1 atomicity: The same thread cannot be interrupted by another thread while it is executing

7.2 Visibility: Be sure to let other threads see the latest results when a thread changes a value volitile: The visibility keyword, which guarantees that the thread must be able to get the most recent change to the main memory value

7.3 The order in which the program executes is executed in the order of the Code

the difference between sleep 8.wait

1:wait is the hair method in object, sleep is the thread method

2:  wait will automatically release the lock, sleep will not

3:wait must be used with the sychronized keyword

9. Deadlock implementation

PackageCom.zhibo.zyw.bdnq.test;Import com.zhibo.zyw.bdnq.test.deadlock.*;/** * @program: Threadproject *  @author : Zyw * @create: 2018-06-26 18:14 **/public class Producerandconsumer { Public static void main ( String[] args) {Thread thread1 = new Thread (new Deadlockthread (truenew Thread (new deadlockthread (false 
 Packagecom.zhibo.zyw.bdnq.test;/*** @program: Threadproject *@author: Zyw * @create: 2018-06-26 18:22 **/ Public classDeadLock {Private StaticObject Monitor1 =NewObject (); Private StaticObject Monitor2 =NewObject ();  Public Static classDeadlockthreadImplementsRunnable {Private Booleantype;  PublicDeadlockthread (Booleantype) {             This. Type =type; } @Override Public voidrun () {if(type) {synchronized(monitor1) {Try{Thread.Sleep (1000); } Catch(interruptedexception e) {e.printstacktrace (); }                }                synchronized(Monitor2) {}}Else {                synchronized(Monitor2) {Try{Thread.Sleep (1000); } Catch(interruptedexception e) {e.printstacktrace (); }                }                synchronized(Monitor1) {}}} }}

Today, the small part of the first to write here, there are any questions can leave a message to Europe!

Java multi-Threading in-depth analysis

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.