Java Summary thread (1)

Source: Internet
Author: User

Java threading is an important item, so it must be mastered as a Java programmer.

Understanding the Java thread must first understand the thread's life cycle in Java. 、

1.java Thread life cycle

1.new creating a thread in Java creates threads in three ways, 1. Inherit the thread class, 2. Inherit the Runnable interface, and then override the Run method of the parent class.

The 2.Runnable thread is in a ready state and can be called by the CPU at any time.

The 3.Running thread is running, and the thread is executing in the CPU.

The 4.Blocked thread is in a blocked state, and for some reason the CPU temporarily interrupts the thread's resources. Only the thread is in the ready state again to be called again by the CPU, while blocking has three states

(1) Wait for blocking: the thread in the running state executes the wait method, causing the thread to enter a wait-blocking state.

(2) Synchronization blocking: The thread acquires the synchronized synchronization lock failure (because the lock is occupied by other threads), it will enter the synchronization blocking state;

(3) Other blocking: When a call to the thread's sleep () or join () or an I/O request is made, the thread enters the blocking state. When the sleep () state times out, join () waits for the thread to terminate or time out, or the I/O process finishes, the thread is re-entered in a ready state.

(4) Death status: The thread finishes running or terminates the thread due to an exception, and the thread ends the life cycle.

2. Three ways to create threads

(1) Inherit the thread class

public class MyThread extends Thread{public MyThread (String name) {super (name);} @Overridepublic void Run () {for (int i=0;i<100;i++) System.out.println (Thread.CurrentThread (). GetName () + "" +i);}}

(2) Inherit runnable interface

public class Myrunnable implements Runnable{private Boolean stop=false, @Overridepublic void Run () {for (int i=0;i<100& amp;&!stop;i++) {System.out.println (Thread.CurrentThread (). GetName () + "" +i);}} public void Stopthread () {this.stop=true;}}

The difference is that the thread inherits the Runnable class, and then inherits the thread class, and the Runnable class is an interface that is implemented by inheriting the Runnable class directly.

At the same time they are created with different

Test Thread:

public void Testthread () {for (int i = 0; i <; i++) {System.out.println (Thread.CurrentThread (). GetName () + "" + i) if (i = =) {MyThread myThread1 = new MyThread ("Window 1"); MyThread myThread2 = new MyThread ("Window 2"); MyThread myThread3 = new MyThread ("Window 3"); MyThread myThread4 = new MyThread ("Window 4"); Mythread1.start (); Mythread2.start (); Mythread3.start (); Mythread4.start ();}}

Test Runnable Method:

public void testrunnable () {int i = 0;for (i = 0; I <=; i++) {System.out.println (Thread.CurrentThread (). GetName () + "" + i), if (i = =) {Runnable myrunnable = new myrunnable (); Thread thread1 = new Thread (myrunnable); Thread thread2 = new Thread (myrunnable); Thread1.start (); Thread2.start ();}}}

As you can see, after inheriting the thread class, you implement the thread creation through new, a Mythread class that inherits thread, and then make the thread ready by calling the start () method in the thread method.

The Runnable method inherits the class new one myrunnable and then puts the myrunnable into a thread of new, and then puts the threads in a ready state through the start () method.

3. Inheriting the callable class

public class Mycallable implements callable<integer> {private int i=0; @Overridepublic Integer call () throws Excepti on {int sum=0;for (; i<100;i++) {System.out.println (Thread.CurrentThread (). GetName () + "" +i); sum+=i;} return sum;}}

Test:

public void testcallable () {callable<integer> mycallable = new mycallable (); futuretask<integer> ft = new Futuretask<> (mycallable), for (int i = 0; I <=; i++) {System.out.println (Th Read.currentthread (). GetName () + "" + i), if (i = =) {Thread thread = new Thread (ft), Thread.Start ();}} System.out.println ("Main thread execution complete"); try {int sum = Ft.get (); System.out.println ("sum=" + sum);} catch (Exception e) {e.printstacktrace ();}}

By testing you will find that inheriting the callable class will be wrapped by the Futuretask class, and then called in the thread, check the API you will see the Futuretask class inherit the future class and inherit the Runnabl class, The difference between the Callabl class and the other two is that the call method has a return parameter.

Java Summary thread (1)

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.