Java multithreading (2) -- thread Creation

Source: Internet
Author: User

Java multithreading (2) -- thread Creation

I. Two Methods for creating threads in java

1. A new Thread class is derived from the java. lang. Thread class, and its run () method is reloaded.

2. Implement the Runnable interface and reload the run () method in the Runnable interface.

Using the Thread class to create a Thread is the same as creating an object of a common class. A Thread is an instance object of the Thread class or its subclass.

Ii. Differences between the two creation threads provided by java

Classes in java are single-inherited. when defining a new Thread class, it can only expand one external class. When the created Thread is inherited from the Thread class, therefore, this thread class can no longer expand other classes and cannot implement complex functions. In this case, if you want to extend other classes of the custom Thread class, you can implement the Runnable interface to implement the Thread class function. At the same time, you can expand other external classes to avoid the limitations of single inheritance.

At the same time, threads created by implementing the Runnable interface can process the same resource to share resources.

3. Thread Creation

(1) derived from the Thread class

The Thread class derived from Thread can be new directly.

For example:

package com.thread.demo;public class JavaThreadDemo {/** * @param args */public static void main(String[] args) {MutliThread m1 = new MutliThread("window 1");MutliThread m2 = new MutliThread("window 2");MutliThread m3 = new MutliThread("window 3");m1.start();m2.start();m3.start();}}class MutliThread extends Thread {private int ticket = 100;MutliThread(String name) {super(name);}public void run() {while(ticket > 0) {ticket--;System.out.println(ticket +" is saled by " + Thread.currentThread().getName());}}}
In the above example, the MutliThread class derived from the Thread class creates three new Thread classes in the main () method and calls the start method of the Thread class to run the Thread, concurrent execution of three threads. The output result is displayed.

(2) Implement the Runnable interface

If the created class implements the Runnable interface, you must use the Thread constructor to create a Thread.

For example:

Package com. thread. demo; public class JavaThreadDemo {/*** @ param args */public static void main (String [] args) {TestRunnable test1 = new TestRunnable ("Zhang San "); testRunnable test2 = new TestRunnable (""); Thread t1 = new Thread (test1); Thread t2 = new Thread (test2); t1.start (); t2.start ();}} class TestRunnable implements Runnable {private String name; TestRunnable (String name) {this. name = name ;}@ Overridepublic void run () {for (int I = 0; I <5; I ++) {try {Thread. sleep (50); // simulate the time-consuming operation System. out. println (name + ":" + I);} catch (InterruptedException e) {e. printStackTrace ();}}}}
In the preceding example, TestRunnable implements the Runnable interface, reloads the run () method, and simulates time-consuming operations in the run () method.

In the main () method, two TestRunnable instances are created first, the Thread constructor is called to create two Thread classes, and the Thread's start () method is called to run the Thread. The two threads run in parallel and the output result is as follows:

Virtual machines are allocated by themselves. The name of the main thread is always main. The name of a non-main thread is unknown, depending on the name allocated by the virtual machine. The names of all threads (including the main thread) can be set and obtained.

2. method for obtaining the object of the current Thread: Thread. currentThread ();

3. Thread execution is not performed in a certain order. For any group of started threads, the scheduling program cannot guarantee the execution sequence and the duration cannot be determined, in the preceding thread example, the results of each running are different, which can be used to illustrate this point.

4. If the run () method of the thread is completed, the thread ends. A running or dead thread can be restarted.

5. Thread Scheduling is part of the JVM. On a CPU machine, only one thread can be run at a time, and only one thread stack can be executed at a time. The JVM thread scheduler determines which thread is running. The sequence of running threads is uncertain.


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.