标题:关于多线程继承Runnable
取消只看楼主
渐渐鱼
Rank: 1
等 级:新手上路
帖 子:80
专家分:0
注 册:2018-5-11
结帖率:90%
已结贴  问题点数:20 回复次数:2 
关于多线程继承Runnable
程序代码:
class Thread1 implements Runnable
{
    int i=10;
    public void run()
    {
        for(;i>0;)
        {
            System.out.printf("day"+"\n");
            i--;
        }
    }
}
class Thread2 implements Runnable
{
    int j=10;
    public void run()
    {
        for(;j>0;)
        {
            System.out.printf("night"+"\n");
            j--;
        }
        
        
    }
}
public class MyThread
{
    public static void main(String []args)
    {
        Thread1 day = new Thread1();
        Thread2 night = new Thread2();
        
        Thread thread1 , thread2;
        thread1 = new Thread(day);
        thread2 = new Thread(night);
        thread1.start();
        thread2.start();
        for(int k=0;k<10;k++)
        {
            System.out.printf("main thread..."+"\n");
        }
    }
}





Thread1 day = new Thread1();
        Thread2 night = new Thread2();
        
        Thread thread1 , thread2;
        thread1 = new Thread(day);
        thread2 = new Thread(night);
        thread1.start();
        thread2.start();

这一段不能直接调用而是thread1 = new Thread(day);样的,小白求教,不懂其中的原理????????
搜索更多相关主题的帖子: 多线程 Thread public new start 
2018-09-12 17:35
渐渐鱼
Rank: 1
等 级:新手上路
帖 子:80
专家分:0
注 册:2018-5-11
得分:0 
回复 2楼 幻紫灵心
你的意思是Thread实例的对象才有start方法,所以不能直接调用,而如果是用了继承Thread这个类,就可以直接调用了?
2018-09-14 09:51
渐渐鱼
Rank: 1
等 级:新手上路
帖 子:80
专家分:0
注 册:2018-5-11
得分:0 
回复 4楼 幻紫灵心
谢啦
2018-09-14 15:05



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




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

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