标题:Runnable与Thread的区别
取消只看楼主
好像小星星
Rank: 2
等 级:论坛游民
帖 子:6
专家分:10
注 册:2016-4-25
 问题点数:0 回复次数:0 
Runnable与Thread的区别
    从别人的博客中看到的关于Runnable与Thread的区别,举了如下代码的例子:
    继承Thread:
    public class ThreadTest {
     public static void main(String[] args) {
        MyThread m1 = new MyThread("一号窗口");
        MyThread m2 = new MyThread("二号窗口");
        MyThread m3 = new MyThread("三号窗口");
        m1.start();
        m2.start();
        m3.start();    }
}
    class MyThread extends Thread{
        private int ticket = 10;
        private String name;
        MyThread(String s){
            this.name = s;
        }
        public void run(){
            for(int i=0;i<10;i++){
                if(this.ticket > 0){
                    System.out.println(Thread.currentThread().getName()+"售票"+ticket--);
                }
            }
        }
    }

    实现Runnable:
    public class ThreadTest {
     public static void main(String[] args) {
        MyThread m1 = new MyThread();
        Thread t1 = new Thread(m1,"一号窗口");
        Thread t2 = new Thread(m1,"二号窗口");
        Thread t3 = new Thread(m1,"三号窗口");
        t1.start();
        t2start();
        t3start();      
   }
}
    class MyThread implements Runnable{
        private int ticket = 10;
        private String name;
        MyThread(String s){
            this.name = s;
        }
        public void run(){
            for(int i=0;i<10;i++){
                if(this.ticket > 0){
                    System.out.println(Thread.currentThread().getName()+"售票"+ticket--);
                }
            }
        }
    }
解释说Runnable是三个人可以完成一件事,Thread是个人做三件事,不知道说的对不对,总感觉不大对,求解释!!!!


[此贴子已经被作者于2016-4-28 22:34编辑过]

搜索更多相关主题的帖子: private public 博客 
2016-04-28 22:30



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-464240-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.088497 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved