1、传统线程创建回顾
2015-08-23 11:34
302 查看
1、直接使用Thread新建线程
运行结果:
2、使用Runnable接口新建线程(从面向对象的角度这方法更好)
运行结果:
3、有趣一个测试
运行结果:
原因:
![](http://images0.cnblogs.com/blog2015/493447/201508/231132330353922.jpg)
但是上面这run方法运行的前提是没有人去重写该方法,而我们上面的代码却重写了,导致上面这图片中的代码无法运行,故运行的是thread的run方法。
package com.yzl; public class ThreadPart_1 { //直接使用Thread对象新建线程并重run方法 public void test1(){ Thread thread1 = new Thread(){ @Override public void run() { super.run(); for(int i=1; i<=10; i++){ System.out.println("Thread:" + Thread.currentThread().getName() + ",value is:" + i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }; thread1.start(); } public static void main(String[] args) { System.out.println("Main Thread:" + Thread.currentThread().getName() + " is run "); ThreadPart_1 test = new ThreadPart_1(); test.test1(); } }
运行结果:
Main Thread:main is run Thread:Thread-0,value is:1 Thread:Thread-0,value is:2 Thread:Thread-0,value is:3 Thread:Thread-0,value is:4 Thread:Thread-0,value is:5 Thread:Thread-0,value is:6 Thread:Thread-0,value is:7 Thread:Thread-0,value is:8 Thread:Thread-0,value is:9 Thread:Thread-0,value is:10
2、使用Runnable接口新建线程(从面向对象的角度这方法更好)
//使用Runnable接口新建线程 public void test2(){ Thread thread = new Thread(new Runnable() { @Override public void run() { for(int i=1; i<=10; i++){ System.out.println("Thread:" + Thread.currentThread().getName() + ",value is:" + i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }); thread.start(); } public static void main(String[] args) { System.out.println("Main Thread:" + Thread.currentThread().getName() + " is run "); ThreadPart_1 test = new ThreadPart_1(); test.test2(); }
运行结果:
Main Thread:main is run Thread:Thread-0,value is:1 Thread:Thread-0,value is:2 Thread:Thread-0,value is:3 Thread:Thread-0,value is:4 Thread:Thread-0,value is:5 Thread:Thread-0,value is:6 Thread:Thread-0,value is:7 Thread:Thread-0,value is:8 Thread:Thread-0,value is:9 Thread:Thread-0,value is:10
3、有趣一个测试
//有趣的测试 public void test3(){ Thread thread = new Thread( new Runnable() { @Override public void run() { System.out.println("Runnable run method is runing~~~"); } } ){ @Override public void run() { System.out.println("Thread run method is runing~~~"); } }; thread.start(); } public static void main(String[] args) { System.out.println("Main Thread:" + Thread.currentThread().getName() + " is run "); ThreadPart_1 test = new ThreadPart_1(); test.test3(); }
运行结果:
Main Thread:main is run Thread run method is runing~~~
原因:
![](http://images0.cnblogs.com/blog2015/493447/201508/231132330353922.jpg)
但是上面这run方法运行的前提是没有人去重写该方法,而我们上面的代码却重写了,导致上面这图片中的代码无法运行,故运行的是thread的run方法。
相关文章推荐
- 经典的js返回(退个页面)
- mac系统在配置navicat时连接数据的时候提示can't connect to mysql server on '127.0.0.1'
- Java环境变量的配置、作用详解
- OJ刷题---University
- 安装新版本Qt Creator后F1无法显示帮助文档的问题
- Android - 警告:it is always overridden by the value specified in the Gradle build script
- 过滤掉URL中的参数部分
- PostgreSQL Replication之第十三章 使用PL/Proxy扩展(2)
- Python学习笔记之动态类型
- nginx+lua+redis构建高并发应用
- 成功靠奋斗,奋斗靠坚持
- linux下安装Android Studo(elementaryOS)
- PostgreSQL Replication之第十三章 使用PL/Proxy扩展(1)
- 计划评审技术(PERT)求工期、标准差、方差以及概率
- 【水题】统计数字
- zoj S-Trees 1150 (二叉树模拟)
- Thinking in java 之:构造器
- SQLite学习笔记(5)-资料整理
- 《AndroidStudio每日一贴》5. 如何快速查看某个方法/注解的定义?
- dofuscator C# 混淆器 原来如此