Explanation of original Java multithreading (1) and original java Multithreading

Source: Internet
Author: User

Explanation of original Java multithreading (1) and original java Multithreading
Reading only is not feasible. For more information, see ~~~~~~ (Specify the source for reference)

 

Let's take a look at the introduction to multithreading in Encyclopedia.

Http://baike.baidu.com/view/65706.htm? Fr = aladdin

 

Java multi-thread support

Three common methods for creating multithreading in Java:

1) inherit the Thread class

Override the run method of the Thread class, create the Thread subclass instance, and start the Thread.

For example:

/** @ Author wxismeit@163.com wangxu */public class TreadOfextends extends Thread {private int I; // override the run () method public void run () {for (I = 0; I <50; I ++) {System. out. println (getName () + "" + I); // when inheriting the Thread class, you can directly use this to obtain the current Thread} public static void main (String [] args) {System. out. println (Thread. currentThread (). getName (); for (int I = 0; I <50; I ++) {if (I = 10) {// call the start () method new TreadOfextends () directly by creating a class object (). start (); new TreadOfextends (). start ();}}}}


 

2) Implement the Runnable interface

Override the run () method and create a Runnable instance as the target of the Thread.

For example:

Public class ThreadOfRun implements Runnable {private int I; // implement the run () method public void run () {for (I = 0; I <50; I ++) in the Runnable interface) {System. out. println (Thread. currentThread (). getName () + "" + I); // multi-threaded implementation through the implementation interface, the current process cannot be obtained through the this keyword} public static void main (String [] args) {for (int I = 0; I <50; I ++) {System. out. println (Thread. currentThread (). getName () + "" + I); if (I = 10) {ThreadOfRun tor = new ThreadOfRun (); // here we need to use the Thread constructor to create a new Thread (tor, "Thread 1 "). start (); new Thread (tor, "Thread 2 "). start ();}}}}


3) After Java 5, you can use more powerful means to implement the Callable interface.

Use the FutureTask object as the target of the Thread object to create and start a new Thread.

Import java. util. concurrent. callable; import java. util. concurrent. futureTask; public class ThreadOfCallble implements Callable <Integer> {// supports generic public Integer call () throws Exception {int I; for (I = 0; I <50; I ++) {System. out. println (Thread. currentThread (). getName () + "" + I);} return I; // return value} public static void main (String [] args) {// create the Callable object ThreadOfCallble toc = new ThreadOfCallble (); // use FutureTask to wrap the Callable object FutureTask <Integer> ft = new FutureTask <Integer> (toc ); for (int I = 0; I <50; I ++) {if (I = 10) {new Thread (ft, "NewThread "). start () ;}try {// get the return value of the new thread System. out. println ("subthread return value:" + ft. get ();} catch (Exception e) {e. printStackTrace ();}}}


Comparison of the three methods: the latter two methods are very suitable for processing the same resource by multiple identical threads. They can separate the CPU, code, and data, which is in line with the object-oriented thinking, it can also inherit other classes, so the last two methods are generally used.

 

Thread life cycle: New and ready state --> running and blocking state --> thread death (cannot be revived ).

(Drag the image if you cannot see it)

 

Join thread

When the join method of another thread is called during execution of a program, the thread of the entry will be blocked until it is executed by the join thread.

<Pre class = "java" name = "code"> public class ThreadOfjoin extends Thread {public ThreadOfjoin (String name) {super (name);} public void run () {for (int I = 0; I <50; I ++) {System. out. println (getName () ;}} public static void main (String [] args) {new ThreadOfjoin ("NewThread "). start (); for (int I = 0; I <50; I ++) {if (I = 10) {ThreadOfjoin toj = new ThreadOfjoin ("JoinedThread "); toj. start (); try {toj. join (); // The main thread calls the join method of toj. It must wait until the main thread of toj executes.} catch (InterruptedException e) {e. printStackTrace () ;}} System. out. println (Thread. currentThread (). getName ());}}}

 

Unfinished: thread priority, thread synchronization, mutex lock, synchronization lock, deadlock, thread communication.

Leave an Email in the comment area to get the Java multi-thread design mode PDF version (automatically crawlers crawl your Email and send it to you through web crawlers)

 

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.