美文网首页
多线程接口Runnable

多线程接口Runnable

作者: 招风小妖怪 | 来源:发表于2019-07-12 08:43 被阅读0次

匿线程方式

class Demo11
{
    public static void main(String s[])
    {
        
        new Thread(new Runnable(){
            public void run()
            {
                while(true)
                {
                    System.out.println(1);      
                }               
            }   
        }).start();
        
    }
}

类实现接口方式

class A implements Runnable
{
    public void run()
    {
        for(int i=0;i<100;i++)
        {
            try
            {
                Thread.sleep(1000);
            }
            catch(Exception e)
            {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
            System.out.print(i+"\t");
        }
    }
}

class Demo11
{
    public static void main(String s[])
    {
        
        A a = new A();
        Thread Tr = new Thread (a);
        Tr.start();
        
    }
}

相关文章

网友评论

      本文标题:多线程接口Runnable

      本文链接:https://www.haomeiwen.com/subject/ffydkctx.html