015.5线程常见用法(面试题)

2018-06-18 02:30:12来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

内容:例子

class ThreadTest 
{
    public static void main(String[] args) 
    {
        /*
        new Thread(){
            public void run(){
                for(int x=0; x<40; x++)
                {
                    System.out.println(Thread.currentThread().getName()+"...X...."+x);
                }
            }
        }.start();
        
        Runnable r= new Runnable(){
            public void run(){
                for(int x=0; x<40; x++)
                {
                    System.out.println(Thread.currentThread().getName()+"...Y...."+x);
                }
            }
        };
        new Thread(r).start();
        
        for(int x=0; x<40; x++)
        {
            System.out.println(Thread.currentThread().getName()+"...Z...."+x);
        }

        System.out.println("Hello World!");
        */
        //面试题:
        new Thread(new Runnable()
        {
            public void run()
            {
                System.out.println("runnable run");
            }
        }){
            public void run()
            {
                System.out.println("subthread run");//执行。
            }
        }.start();
    }
}
/*
class Thread 
{
    private Runnable r;
    Thread(Runnable r)
    {
        this.r = r;
    }
    public void run()
    {
        if(r!=null)
        {
            r.run();
        }
    }
    public void start()
    {
        run();
    }
}
class SubThread extends Thread
{
    public void run()
    {
        System.out.println("subthread run");
    }
}

Runnable r = new Runnable()
{
    public void run()
    {
        System.out.println("runnable run");
    }
}
SubThread t = new SubThread(r);
t.start();

*/

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:JDBC的批处理

下一篇:Spring Boot 表单验证、AOP统一处理请求日志、单元测试