Java:thread and runnable threading classes

Source: Internet
Author: User

Java:thread and runnable threading classes

Java has 2 ways to implement threading: The Thread class, the Runnable interface. (In fact, thread itself is runnable subclass)

The thread class, which defaults to the run (), the Start () method, inherits the thread class and requires the Run method to be implemented

Thread multi-threading, can not share resources, ensure the unification of data (in order to store the number of goods, ticket system ticket number as an example)

For example:

public class MyThread extends Thread {private String name;//define Name Property public MyThread (String name) {this.name = name;} public void Run () {//overwrite run () method for (int i = 0; i < i++) {//= Loop 10 times System.out.println ("Thread run:" + name + ", i =" + i);}}}

Implementation 1:

This method demonstrates that the MT1 thread is executed before the MT2 thread can be executed

public class ThreadDemo01 {public static void main (string[] args) {MyThread mt1 = new MyThread ("Thread A"); MyThread mt2 = new MyThread ("Thread B"); Mt1.run ();//Call Thread body mt2.run ();//Call Thread Body}}

  

Implementation 2:

The start () method indicates that invoking the system's underlying method to preempt CPU resources, who first grab, who will be given priority to the CPU scheduling

public class ThreadDemo02 {public static void main (string[] args) {MyThread mt1 = new MyThread ("Thread A"); MyThread mt2 = new MyThread ("Thread B"); Mt1.start (); Call Thread body Mt2.start (); Call Thread Body}}

  

Runnable:

public class MyThread implements Runnable {//Implement Runnable interface private String name;//define Name Property public MyThread (String name) {T His.name = name;} public void Run () {//overwrite run () method for (int i = 0; i < i++) {//= Loop 10 times System.out.println ("Thread run:" + name + ", i =" + i);}}}

  

Implementing subclasses:

public class RunnableDemo01 {public static void main (string[] args) {MyThread mt1 = new MyThread ("Thread A"); MyThread mt2 = new MyThread ("Thread B"); New Thread (MT1). Start (); Call the thread body new thread (MT2). Start (); Call Thread Body}}

  

It is recommended to implement the Runnable interface to use multithreading.

Use runnable to achieve resource sharing, take the mall/ticketing system for example, sell tickets

Thread Example:

public class MyThread extends thread {//inherits the thread class private int ticket = 5;//altogether 5 tickets public void run () {//overwrite the Run () method for (int i = 0; I < 50; i++) {//= Loop 10 times if (This.ticket > 0) {System.out.println ("sell ticket: Ticket =" + this.ticket--);}}}

  

Implementing subclasses:

This method: In fact, there are only 5 tickets, but three threads, each thread can buy 5 tickets, this is not logical

public class Threadticket {public static void main (string[] args) {MyThread mt1 = new MyThread ();//a thread MyThread mt2 = NE W MyThread (); A thread MyThread mt3 = new MyThread (); A thread mt1.start ();//Start Selling tickets Mt2.start ();//Start Selling tickets Mt3.start ();//Start selling tickets}}

  

Implementation method Two:

public class MyThread implements Runnable {//implement Runnable interface private int ticket = 5;//altogether 5 tickets public void run () {//overwrite run () method for (int i = 0; i < i++) {//= Loop 10 times if (This.ticket > 0) {System.out.println ("sell ticket: Ticket =" + this.ticket--);}} }}

  

Implementing subclasses:

This method enables the sharing of resources within a thread and does not sell extra tickets

public static void Main (string[] args) {MyThread mt = new MyThread ();//An Object new Thread (MT). Start ();//A thread starts selling ticket new thread (MT). Start ();//A thread
New Thread (MT). Start (); A thread starts selling tickets
}

  

 

Java:thread and runnable threading classes

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.