Java Foundation Enhancement Concurrency (ii) common multithreading implementation methods

Source: Internet
Author: User
Tags thread class ticket

Overview

There are 2 common ways to implement multithreading:

1. Inheriting the thread class

2. Implement the Runnable interface

This is commonly used because multithreading can also be achieved through the thread pool in the Juc (java.util.concurrent) package. We'll go into details about the thread pool, and now we'll get to the thread and runnable.

Thread Introduction

Thread is a class. Thread itself implements the Runnable interface. It declares the following:

 Public class Implements Runnable {}

The role of thread is to implement multithreading.

Threadtest.java Source

classMyThreadextendsthread{Private intticket=10;  Public voidrun () { for(inti=0;i<20;i++){             if( This. ticket>0) {System.out.println ( This. GetName () + "Sell ticket: Ticket" + This. ticket--); }        }    } }; Public classThreadTest { Public Static voidMain (string[] args) {//start 3 thread t1,t2,t3, and sell 10 tickets per thread! MyThread t1=NewMyThread (); MyThread T2=NewMyThread (); MyThread T3=NewMyThread ();        T1.start ();        T2.start ();    T3.start (); }  }
View Code

Run results

Thread-0Sell Tickets: Ticket10thread-1Sell Tickets: Ticket10thread-2Sell Tickets: Ticket10thread-1Sell Tickets: Ticket9thread-0Sell Tickets: Ticket9thread-1Sell Tickets: Ticket8thread-2Sell Tickets: Ticket9thread-1Sell Tickets: Ticket7thread-0Sell Tickets: Ticket8thread-1Sell Tickets: Ticket6thread-2Sell Tickets: Ticket8thread-1Sell Tickets: Ticket5thread-0Sell Tickets: Ticket7thread-1Sell Tickets: Ticket4thread-2Sell Tickets: Ticket7thread-1Sell Tickets: Ticket3thread-0Sell Tickets: Ticket6thread-1Sell Tickets: Ticket2thread-2Sell Tickets: Ticket6thread-2Sell Tickets: Ticket5thread-2Sell Tickets: Ticket4thread-1Sell Tickets: Ticket1thread-0Sell Tickets: Ticket5thread-2Sell Tickets: Ticket3thread-0Sell Tickets: Ticket4thread-2Sell Tickets: Ticket2thread-0Sell Tickets: Ticket3thread-2Sell Tickets: Ticket1thread-0Sell Tickets: Ticket2thread-0 Sell Tickets: ticket1
View Coderunnable Introduction

Runnable is a functional interface that contains only a run () method. It is defined as follows:

@FunctionalInterface  Public Interface Runnable {    publicabstractvoid  run ();}

runnable function, realize multithreading. We can define a class A to implement the Runnable interface, and then create a new thread by means of the new thread (new A ()).

Runnabletest.java Source

classMyThreadImplementsrunnable{Private intticket=10;  Public voidrun () { for(inti=0;i<20;i++){             if( This. ticket>0) {System.out.println (Thread.CurrentThread (). GetName ()+ "Sell ticket: Ticket" + This. ticket--); }        }    } };  Public classRunnabletest { Public Static voidMain (string[] args) {MyThread Mt=NewMyThread (); //Start 3 Threads t1,t2,t3 (they share a runnable object), and these 3 threads sell 10 tickets altogether! Thread t1=NewThread (MT); Thread T2=NewThread (MT); Thread T3=NewThread (MT);        T1.start ();        T2.start ();    T3.start (); }  }
View Code

Run results

Thread-0 Sell ticket: Ticket10thread-2 Sell ticket: Ticket8thread-1 Sell ticket: Ticket9thread-2 Sell ticket: Ticket6thread -0 Sell ticket: Ticket7thread-2 Sell ticket: Ticket4thread-1 Sell ticket: Ticket5thread-2 Sell ticket: Ticket2thread -0 Sell ticket: Ticket3thread-1 sell ticket: Ticket1
View CodeThe similarities and differences between thread and runnable

the same point of thread and Runnable : "Multithreading is the way to implement."
different points of Thread and Runnable :
Thread is a class, and runnable is an interface; the thread itself is the class that implements the Runnable interface. We know that "a class can have only one parent class, but it can implement multiple interfaces," So runnable has better extensibility.
In addition, runnable can also be used for "resource sharing". That is, multiple threads are built on a Runnable object that shares the resources on the Runnable object.
In general, it is recommended to implement multithreading through "Runnable"!

Java Foundation Enhancement Concurrency (ii) common multithreading implementation methods

Related Article

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.