java 多線程一

來源:互聯網
上載者:User

標籤:ext   creat   err   print   nts   ati   java 多線程   try   exce   

java 多線程實現的幾種方式:

1、extends Thread

2、implements Runnable

3、implements Callable<>

下面上代碼:

import java.util.concurrent.Callable;import java.util.concurrent.FutureTask;/** * Created by root on 17-9-30. */public class Test4Thread {    public static void main(String[] args) throws Exception {        //**********************        MyThread1 myThread1_1=new MyThread1("myThread1_1");        MyThread1 myThread1_2=new MyThread1("myThread1_2");        MyThread1 myThread1_3=new MyThread1("myThread1_3");        myThread1_1.start();        myThread1_2.start();        myThread1_3.start();        //***********************        MyThread2 myThread2 = new MyThread2();        Thread t2_1 = new Thread(myThread2);        Thread t2_2 = new Thread(myThread2);        Thread t2_3 = new Thread(myThread2);        t2_1.setName("MyThread2_1");        t2_2.setName("MyThread2_2");        t2_3.setName("MyThread2_3");        t2_1.start();        t2_2.start();        t2_3.start();        //**************************        MyThread3 myThread3=new MyThread3();        FutureTask<String> futureTask1=new FutureTask<String>(myThread3);        FutureTask<String> futureTask2=new FutureTask<String>(myThread3);        FutureTask<String> futureTask3=new FutureTask<String>(myThread3);        Thread t3_1 = new Thread(futureTask1);        Thread t3_2 = new Thread(futureTask2);        Thread t3_3 = new Thread(futureTask3);        t3_1.setName("MyThread3_1");        t3_2.setName("MyThread3_2");        t3_3.setName("MyThread3_3");        t3_1.start();        t3_2.start();        t3_3.start();        System.out.println(futureTask1.get());        System.out.println(futureTask2.get());        System.out.println(futureTask3.get());    }}class MyThread1 extends Thread {    int tickets = 5;    public MyThread1(String name) {        super(name);    }    @Override    public void run() {        for (; tickets > 0; ) {            try {                Thread.sleep(1000);            } catch (InterruptedException e) {                e.printStackTrace();            }            System.out.println(Thread.currentThread().getName() + ":" + tickets--);        }    }}class MyThread2 implements Runnable {    int tickets = 5;    public void run() {        for (; tickets > 0; ) {            try {                Thread.sleep(1000);            } catch (InterruptedException e) {                e.printStackTrace();            }            System.out.println(Thread.currentThread().getName() + ":" + tickets--);        }    }}class MyThread3 implements Callable<String> {    int tickets=5;    public String call() throws Exception {        for (; tickets > 0; ) {            try {                Thread.sleep(1000);            } catch (InterruptedException e) {                e.printStackTrace();            }            System.out.println(Thread.currentThread().getName() + ":" + tickets--);        }        return Thread.currentThread().getName() + ":" +"賣完了";    }}

 運行結果:

myThread1_1:5myThread1_2:5myThread1_3:5myThread1_1:4myThread1_3:4myThread1_2:4myThread1_1:3myThread1_2:3myThread1_3:3myThread1_1:2myThread1_3:2myThread1_2:2myThread1_1:1myThread1_3:1myThread1_2:1

 

MyThread2_1:5MyThread2_2:5MyThread2_3:4MyThread2_1:3MyThread2_2:2MyThread2_3:1MyThread2_1:0MyThread2_2:-1

 

MyThread3_1:5MyThread3_2:4MyThread3_3:3MyThread3_1:2MyThread3_2:1MyThread3_1:賣完了MyThread3_2:賣完了MyThread3_3:0MyThread3_3:賣完了

 

可以看到,後在需要實現多線程操作公工資料時最好用Runable 、Callable介面的方式,當然也可以用Thread (類似Runable的方式實現)。

java 多線程一

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.