首页 > 编程语言 > 详细

(黑马Java多线程与并发库高级应用)01 传统线程技术回顾

时间:2017-07-23 10:42:18      阅读:208      评论:0      收藏:0      [点我收藏+]

传统创建线程的两种方式

package cn.itcast.heima2;

public class TraditionalThread {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Thread thread = new Thread() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName());
                    System.out.println(this.getName());
                }
            }
        };
        thread.start();

        Thread thread2 = new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName());
                }
            }
        });
        thread2.start();//更可以体现面向对象的思想,线程和代码隔离
        
        
        new Thread(new Runnable() {
            
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println("runnable:"+Thread.currentThread().getName());
                }
            }
        }){
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println("thread:"+Thread.currentThread().getName());
                }
            }
        }.start();
    }

}

 

(黑马Java多线程与并发库高级应用)01 传统线程技术回顾

原文:http://www.cnblogs.com/404-not-found/p/7223852.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!