Thread 和 Runnable 的一些区别
2016-06-06 15:09
190 查看
package com.lsh.java8.thread; import java.util.concurrent.atomic.AtomicInteger; public class MySimpleRunnable implements Runnable { private static AtomicInteger index = new AtomicInteger(); private AtomicInteger value = new AtomicInteger(10); @Override public void run() { for(int i = 0 ; i < 10 && value.get() >= 0; i++) { System.out.println(Thread.currentThread().getName() + ": value = " + value.getAndDecrement()); } } public static void main(String[] args) { Thread thread = new Thread(new MySimpleRunnable()); new Thread(thread, "My_runnable" + index.getAndIncrement()).start(); new Thread(thread, "My_runnable" + index.getAndIncrement()).start(); new Thread(thread, "My_runnable" + index.getAndIncrement()).start(); } }
console
My_runnable0: value = 10
My_runnable0: value = 7
My_runnable2: value = 8
My_runnable2: value = 5
My_runnable2: value = 4
My_runnable2: value = 3
My_runnable2: value = 2
My_runnable2: value = 1
My_runnable2: value = 0
My_runnable1: value = 9
My_runnable0: value = 6
package com.lsh.java8.thread; import java.util.concurrent.atomic.AtomicInteger; public class MySimpleThread extends Thread { private static AtomicInteger index = new AtomicInteger(); private AtomicInteger value = new AtomicInteger(10); @Override public void run() { for(int i = 0 ; i < 10 && value.get() >= 0; i++) { System.out.println( this.getName() + ": value = " + value.getAndDecrement()); } } public MySimpleThread(String name) { this.setName(name + index.getAndIncrement()); } public static void main(String[] args) { MySimpleThread thread1 = new MySimpleThread("My_thread_"); MySimpleThread thread2 = new MySimpleThread("My_thread_"); MySimpleThread thread3 = new MySimpleThread("My_thread_"); thread1.run(); thread2.run(); thread3.run(); } }
console
My_thread_0: value = 10
My_thread_0: value = 9
My_thread_0: value = 8
My_thread_0: value = 7
My_thread_0: value = 6
My_thread_0: value = 5
My_thread_0: value = 4
My_thread_0: value = 3
My_thread_0: value = 2
My_thread_0: value = 1
My_thread_1: value = 10
My_thread_1: value = 9
My_thread_1: value = 8
My_thread_1: value = 7
My_thread_1: value = 6
My_thread_1: value = 5
My_thread_1: value = 4
My_thread_1: value = 3
My_thread_1: value = 2
My_thread_1: value = 1
My_thread_2: value = 10
My_thread_2: value = 9
My_thread_2: value = 8
My_thread_2: value = 7
My_thread_2: value = 6
My_thread_2: value = 5
My_thread_2: value = 4
My_thread_2: value = 3
My_thread_2: value = 2
My_thread_2: value = 1
相关文章推荐
- Arun Jaitley:要健康最好让银行保持增长势头
- RNN-LSTM
- 14.2.5.7 Physical Row Structure 物理数据结构:
- 14.2.5.7 Physical Row Structure 物理数据结构:
- iPad开发--UIPopoverController简单使用iOS7之前和iOS7之后的使用方法
- 后台获取调用Ajax
- 浅析AST抽象语法树及Python代码实现
- Linux 安装 Kafka 集群
- android多线程编程之AsyncTask与Handler
- Java生成plist下载ipa文件
- Android 避免Overdraw 过度绘制
- javaScript学习笔记---入门
- a标签截字
- 在线编辑网页jsbin
- 拍照权限导致某些代码在6.0系统上崩溃
- easyui 客户端排序
- JavaScript学习9张思维导图
- CentOS 6.4 中安装php5.2.17 的方法
- pycharm 快捷键
- Maven安装Nexus私服说明