package com; public class TraditionalThread { public static void main(String[] args) { Thread thread = new Thread() { @Override public void run() { while (true) { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out .println("1: " + Thread.currentThread().getName()); System.out.println("2: " + this.getName()); } } }; thread.start(); Thread thread2 = new Thread(new Runnable() { @Override public void run() { while (true) { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out .println("1: " + Thread.currentThread().getName()); System.out .println("2: " + Thread.currentThread().getName()); } } }); thread2.start(); new Thread( new Runnable() { @Override public void run() { while (true) { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out .println("Runnable: " + Thread.currentThread().getName()); } } } ){ public void run() { while (true) { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out .println("Thread: " + Thread.currentThread().getName()); } } }.start(); } }
代码量提升之路-Java多线程
原创
©著作权归作者所有:来自51CTO博客作者foxspark的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:代码量提升之路 - 接口练习
下一篇:代码量提升之路 - 定时器
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章