码迷,mamicode.com
首页 > 编程语言 > 详细

Java 多线程的两种实现方式

时间:2015-11-19 00:20:59      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

继承Thead

class IphoneThread extends Thread {
    public int iphone = 5;
    public String user;

    public IphoneThread(String str) {
        user = str;
    }

    @Override
    public void run() {
        while (iphone != 0) {
            iphone--;
            System.out.println(user + " get one and left iphone num=" + iphone);
        }
    }
}

public class ThreadTest {
    public static void main(String[] arg) {
        IphoneThread t1 = new IphoneThread("Toms");
        t1.start();
        IphoneThread t2 = new IphoneThread("Jack");
        t2.start();
        IphoneThread t3 = new IphoneThread("Boom");
        t3.start();
    }
}

实现Runnable接口

class IphoneRunable implements Runnable {

    public int iphone = 5;

    @Override
    public void run() {
        while (iphone != 0) {
            iphone--;
            System.out.println(Thread.currentThread().getName()
                    + " get one and left iphone num=" + iphone);
        }

    }

}

public class RunableTest {
    public static void main(String[] arg) {
        IphoneRunable runable = new IphoneRunable();
        Thread t1 = new Thread(runable, "Toms");
        t1.start();
        Thread t2 = new Thread(runable, "Jack");
        t2.start();
        Thread t3 = new Thread(runable, "Boom");
        t3.start();
    }
}

 

Java 多线程的两种实现方式

标签:

原文地址:http://www.cnblogs.com/mingziday/p/4976213.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!