Java multithreading comprehension, java Multithreading

Source: Internet
Author: User

Java multithreading comprehension, java Multithreading

I. theoretical concepts

Processes and threads

A Process is a running activity of a program on a dataset in a computer. It is the basic unit for the system to allocate and schedule resources.

A thread is an entity of a process. It is the basic unit for CPU scheduling and scheduling. It is smaller than a process and can run independently. the thread itself basically does not have system resources, and only has a few resources (such as program counters, a set of registers and stacks) that are essential for running ), however, it can share all resources of a process with other threads of the same process.

A process has an independent address space. After a process crashes, it will not affect other processes in the protection mode, but the thread is only a different execution path in the process. A thread has its own stack and local variables, but there is no separate address space between threads. If a thread dies, the whole process dies. Therefore, multi-process programs are more robust than multi-threaded programs, however, during process switching, resources are consumed and the efficiency is lower.But for some concurrent operations that require simultaneous and shared variables, you can only use threads, not processes.

 

Ii. Java implementation of threads

Currently, java threads can be implemented in two ways: Inherit the Thread class, implement the Runnable interface, and override the run method.

1. Implement the Runnable interface

The benefit of implementing the Runnable interface is thatVariable sharingMultiple Threads use the same Runable object. The member variable j in the code block is as follows:

Package src;/***** @ author Shawn Wang **/public class MyThread implements Runnable {private int j = 100; @ Override public void run () {while (true) {if (j> 0) {System. out. println (Thread. currentThread (). getName () + "-- num:" + j --) ;}else {break ;}} public static void main (String [] args) {MyThread mt = new MyThread (); Thread thread1 = new Thread (mt); Thread thread2 = new Thread (mt); Thread thread3 = new Thread (mt); thread1.start (); thread2.start (); thread3.start ();}}View Code

Output result:

Thread-1 -- num: 100Thread-3 -- num: 98Thread-2 -- num: 99Thread-2 -- num: 95Thread-3 -- num: 96Thread-1 -- num: 97Thread-1 -- num: 92Thread-1 -- num: 91Thread-3 -- num: 93Thread-3 -- num: 89Thread-3 -- num: 88Thread-2 -- num: 94Thread-2 -- num: 86Thread-2 -- num: 85Thread-2 -- num: 84Thread-3 -- num: 87Thread-3 -- num: 82Thread-1 -- num: 90Thread-1 -- num: 80Thread-3 -- num: 81Thread-3 -- num: 78Thread-3 -- num: 77Thread-3 -- num: 76Thread-3 -- num: 75Thread-3 -- num: 74Thread-2 -- num: 83Thread-2 -- num: 72Thread-2 -- num: 71Thread-2 -- num: 70Thread-3 -- num: 73Thread-1 -- num: 79Thread-3 -- num: 68Thread-3 -- num: 66Thread-3 -- num: 65Thread-2 -- num: 69Thread-2 -- num: 63Thread-2 -- num: 62Thread-2 -- num: 61Thread-2 -- num: 60Thread-2 -- num: 59Thread-2 -- num: 58Thread-2 -- num: 57Thread-2 -- num: 56Thread-2 -- num: 55Thread-2 -- num: 54Thread-2 -- num: 53Thread-2 -- num: 52Thread-3 -- num: 64Thread-1 -- num: 67Thread-3 -- num: 50Thread-2 -- num: 51Thread-3 -- num: 48Thread-1 -- num: 49Thread-3 -- num: 46Thread-2 -- num: 47Thread-3 -- num: 44Thread-1 -- num: 45Thread-3 -- num: 42Thread-2 -- num: 43Thread-3 -- num: 40Thread-1 -- num: 41Thread-3 -- num: 38Thread-2 -- num: 39Thread-3 -- num: 36Thread-1 -- num: 37Thread-3 -- num: 34Thread-2 -- num: 35Thread-3 -- num: 32Thread-1 -- num: 33Thread-3 -- num: 30Thread-2 -- num: 31Thread-3 -- num: 28Thread-1 -- num: 29Thread-3 -- num: 26Thread-2 -- num: 27Thread-3 -- num: 24Thread-1 -- num: 25Thread-3 -- num: 22Thread-2 -- num: 23Thread-3 -- num: 20Thread-1 -- num: 21Thread-3 -- num: 18Thread-2 -- num: 19Thread-3 -- num: 16Thread-1 -- num: 17Thread-3 -- num: 14Thread-2 -- num: 15Thread-3 -- num: 12Thread-1 -- num: 13Thread-3 -- num: 10Thread-2 -- num: 11Thread-3 -- num: 8Thread-1 -- num: 9Thread-3 -- num: 6Thread-2 -- num: 7Thread-3 -- num: 4Thread-1 -- num: 5Thread-3 -- num: 2Thread-2 -- num: 3Thread-1 -- num: 1Result

2. inherit the Thread class

Package src; public class ThreadClient extends Thread {public ThreadClient (String name) {super (name) ;}@ Override public void run () {// TODO Auto-generated method stub for (int I = 0; I <= 10; I ++) {System. out. println (currentThread (). getName () + ":" + I) ;}} public static void main (String [] args) {ThreadClient thread1 = new ThreadClient ("thread1 "); threadClient thread2 = new ThreadClient ("thread1"); ThreadClient thread3 = new ThreadClient ("thread1"); thread1.start (); thread2.start (); thread3.start ();}}View Code

Output result

Thread1: 0thread1: 1thread1: 0thread1: 1thread1: Unknown: 1thread1: 2thread1: Unknown: 6thread1: 2thread1: 2thread1: 3thread1: Unknown: 3thread1: 4thread1: 5thread1: 6thread1: 7thread1: 7thread1: 8thread1: 9thread1: 10thread1: 5thread1: 6thread1: 7thread1: 8thread1: 9thread1: 8thread1: 9thread1: 10thread1: 10Result

Question:

Why rewrite the run method?

The run method stores the code to be run by the thread. You cannot directly call the run method. You must use the start method to call it.

Iii. Thread Synchronization

Scenario: If the thread performs sleep operations, other threads change the shared variables, causing data non-synchronization problems.

Package src;/***** @ author Shawn Wang **/public class MyThread implements Runnable {private int j = 100; @ Override public void run () {while (true) {if (j> 0) {try {Thread. sleep (1000); // The current thread sleeps for 1 second} catch (Exception e) {e. printStackTrace ();} System. out. println (Thread. currentThread (). getName () + "-- num:" + j --) ;}else {break ;}} public static void main (String [] args) {MyThread mt = new MyThread (); Thread thread1 = new Thread (mt); Thread thread2 = new Thread (mt); Thread thread3 = new Thread (mt); thread1.start (); thread2.start (); thread3.start ();}}View Code

Output result:

Thread-1 -- num: 100Thread-2 -- num: 99Thread-3 -- num: 98Thread-1 -- num: 97Thread-3 -- num: 95Thread-2 -- num: 96Thread-1 -- num: 94Thread-2 -- num: 92Thread-3 -- num: 93Thread-3 -- num: 91Thread-2 -- num: 90Thread-1 -- num: 90Thread-3 -- num: 89Thread-2 -- num: 88Thread-1 -- num: 87Thread-3 -- num: 86Thread-2 -- num: 85Thread-1 -- num: 85Thread-3 -- num: 84Thread-2 -- num: 83Thread-1 -- num: 82Thread-3 -- num: 81Thread-1 -- num: 80Thread-2 -- num: 80Thread-3 -- num: 79Thread-2 -- num: 77Thread-1 -- num: 78Thread-3 -- num: 76Thread-2 -- num: 75Thread-1 -- num: 74Thread-2 -- num: 73Thread-3 -- num: 71Thread-1 -- num: 72Thread-2 -- num: 70Thread-1 -- num: 69Thread-3 -- num: 69Thread-2 -- num: 68Thread-3 -- num: 66Thread-1 -- num: 67Thread-2 -- num: 65Thread-1 -- num: 64Thread-3 -- num: 64Thread-2 -- num: 63Thread-1 -- num: 62Thread-3 -- num: 61Thread-2 -- num: 60Thread-3 -- num: 58Thread-1 -- num: 59Thread-2 -- num: 57Thread-1 -- num: 56Thread-3 -- num: 56Thread-2 -- num: 55Thread-1 -- num: 54Thread-3 -- num: 54Thread-2 -- num: 53Thread-3 -- num: 52Thread-1 -- num: 52Thread-3 -- num: 51Thread-2 -- num: 50Thread-1 -- num: 51Thread-3 -- num: 49Thread-1 -- num: 48Thread-2 -- num: 48Thread-3 -- num: 47Thread-1 -- num: 46Thread-2 -- num: 45Thread-1 -- num: 44Thread-2 -- num: 42Thread-3 -- num: 43Thread-3 -- num: 40Thread-1 -- num: 39Thread-2 -- num: 41Thread-2 -- num: 38Thread-3 -- num: 37Thread-1 -- num: 38Thread-1 -- num: 36Thread-2 -- num: 35Thread-3 -- num: 36Thread-1 -- num: 34Thread-2 -- num: 33Thread-3 -- num: 33Thread-1 -- num: 32Thread-3 -- num: 30Thread-2 -- num: 31Thread-1 -- num: 29Thread-3 -- num: 29Thread-2 -- num: 29Thread-3 -- num: 28Thread-1 -- num: 28Thread-2 -- num: 28Thread-1 -- num: 27Thread-2 -- num: 26Thread-3 -- num: 27Thread-1 -- num: 25Thread-2 -- num: 24Thread-3 -- num: 24Thread-1 -- num: 23Thread-3 -- num: 22Thread-2 -- num: 22Thread-1 -- num: 21Thread-3 -- num: 20Thread-2 -- num: 20Thread-1 -- num: 19Thread-3 -- num: 18Thread-2 -- num: 17Thread-3 -- num: 16Thread-1 -- num: 15Thread-2 -- num: 14Thread-2 -- num: 13Thread-3 -- num: 12Thread-1 -- num: 13Thread-2 -- num: 11Thread-1 -- num: 10Thread-3 -- num: 10Thread-2 -- num: 9Thread-1 -- num: 7Thread-3 -- num: 8Thread-2 -- num: 6Thread-3 -- num: 5Thread-1 -- num: 5Thread-2 -- num: 4Thread-3 -- num: 3Thread-1 -- num: 2Thread-2 -- num: 1Thread-1 -- num: 0Thread-3 -- num:-1Result

A negative number is displayed.

Fix synchronization lockSynchronized

Synchronous Code block format:
Synchronized (object ){
Code to be synchronized;
}

Package src;/***** @ author Shawn Wang **/public class MyThread implements Runnable {private int j = 100; @ Override public void run () {while (true) {synchronized (this) {if (j> 0) {try {Thread. sleep (1000); // The current thread sleeps for 1 second} catch (Exception e) {e. printStackTrace ();} System. out. println (Thread. currentThread (). getName () + "-- num:" + j --);} else {break ;}}} public static void main (String [] args) {MyThread mt = new MyThread (); Thread thread1 = new Thread (mt); Thread thread2 = new Thread (mt); Thread thread3 = new Thread (mt); thread1.start (); thread2.start (); thread3.start ();}}View Code

 

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.