张孝祥[致敬]-多线程学习第01课 传统线程的创建
2016-11-02 23:25
363 查看
本人地址:http://blog.csdn.net/hblfyla/article/details/53014756
什么是线程?
线程就是程序执行的不同分支
传统创建线程的两种方式
1.继承Thread类,本次采用面向对象的写法,所有代码都封装在一个对象里面
代码:
运行:
2.实现Runnable接口,本次采用面向对象的写法,所有代码都封装在一个对象里面
代码:
运行:
思考题:如下代码写法会运行什么结果?
代码:
运行:
本人地址:http://blog.csdn.net/hblfyla/article/details/53014756
什么是线程?
线程就是程序执行的不同分支
传统创建线程的两种方式
1.继承Thread类,本次采用面向对象的写法,所有代码都封装在一个对象里面
代码:
package org.yla.zxx.day01; /** * 复习创建线程的方式 * @author yangluan * 2016年11月2日下午11:05:23 */ public class TraditionalThreadDemo01 { public static void main(String[] args) { //继承Thread类 Thread t = new Thread(){ public void run() { while(true){ try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("thread->1: "+Thread.currentThread().getName()); System.out.println("thread->2: "+this.getName()); } }; }; t.start(); } }
运行:
thread->1: Thread-0 thread->2: Thread-0 thread->1: Thread-0 thread->2: Thread-0 thread->1: Thread-0 thread->2: Thread-0 thread->1: Thread-0 thread->2: Thread-0
2.实现Runnable接口,本次采用面向对象的写法,所有代码都封装在一个对象里面
代码:
package org.yla.zxx.day01; /** * 传统创建线程复习 * @author yangluan * 2016年11月2日下午11:09:44 */ public class TraditionalThreadDemo02 { public static void main(String[] args) { //实现Runnalable接口 Thread t = new Thread(new Runnable() { @Override public void run() { while(true){ try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Runnable->1: "+Thread.currentThread().getName()); } } }); t.start(); } }
运行:
Runnable->1: Thread-0 Runnable->1: Thread-0 Runnable->1: Thread-0 Runnable->1: Thread-0 ........
思考题:如下代码写法会运行什么结果?
代码:
package org.yla.zxx.day01; /** * 传统创建线程复习 * @author yangluan * 2016年11月2日下午11:09:44 */ public class TraditionalThreadDemo03 { public static void main(String[] args) { 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(); } //new Thread(new Runnable(){run()}){run()}.start(); //子类对象覆写父类对象方法,所以调用的是子类对象的方法 }
运行:
thread: Thread-0 thread: Thread-0 thread: Thread-0 thread: Thread-0 thread: Thread-0 thread: Thread-0 .........
本人地址:http://blog.csdn.net/hblfyla/article/details/53014756
相关文章推荐
- 张孝祥[致敬]-多线程学习第02课 传统定时器的使用
- 多线程学习-传统线程技术创建
- 张孝祥[致敬]-多线程学习第05课-线程范围内的共享变量
- 张孝祥[致敬]-多线程学习第06课-多个线程之间的共享变量
- 张孝祥[致敬]-多线程学习第04课-线程的通信机制
- 马士兵-多线程学习第01课 线程的创建和启动
- 张孝祥[致敬]-多线程学习第03课-线程的互斥与同步
- IOS学习之多线程(2)--创建线程
- 6.多线程学习--守护线程的创建和运行
- Java学习之多线程--创建线程的三种方式
- Java多线程学习总结--线程概述及创建线程的方式(1)
- java多线程之创建线程的两种传统方式
- 【java多线程与并发库】---传统java多线程<2> 线程创建方式
- JAVA基础学习(十一)--多线程一线程的创建,运行,同步和锁
- 张孝祥java.concurrent线程并发学习笔记 - 传统线程回顾
- Java6学习笔记56——多线程编程——线程的创建方法2
- 多线程(一) —— 传统线程(Thread)的创建和计时器(Timer)
- 多线程学习-线程创建与销毁摘录
- 多线程学习1-创建线程
- 11.多线程学习--用线程工厂创建线程