Java Multi-Threading implementation

Source: Internet
Author: User
Tags thread class

Implementing multithreading by inheriting the thread class

 Public classhello{ Public Static voidMain (String args[]) {MyThread TR1=NewMyThread ("Thread 1"); MyThread TR2=NewMyThread ("Thread 2"); MyThread TR3=NewMyThread ("Thread 3");        Tr1.start ();        Tr2.start ();    Tr3.start (); }}classMyThreadextendsthread{PrivateString name;  PublicMyThread (String name) { This. Name =name; } @Override Public voidrun () { for(intx=0; x<10; X + +) {System.out.println ( This. name+ ", x=" +x); }    }}

multithreading through the Runnable interface

public class hello{public    static void Main (String args[]) {        MyThread mt1 = new MyThread ("Thread A");        MyThread mt2 = new MyThread ("Thread B");        MyThread mt3 = new MyThread ("Thread C");        Thread TR1 = new Thread (MT1);        Thread TR2 = new Thread (MT2);        Thread TR3 = new Thread (MT3);        Tr1.start ();        Tr2.start ();        Tr3.start ();    }} Class MyThread extends thread{    private String name;    Public MyThread (String name) {        this.name = name;    }    @Override public    Void Run () {        for (int x=0; x<10; x + +) {            System.out.println (this.name+ ", x=" +x);        }    }}

  

Java Multi-Threading implementation

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.