多线程基础 定义线程类 · 启动线程 · 停止线程
2015-07-06 14:29
567 查看
一、构造多线程类
1、继承Thread类
[code]package multithread; public class TestThread01 { public static void main(String[] args) { Thread th = new FirstThread(); th.start(); for(int i=0; i<100; i++){ System.out.println("main:"+i); } } } class FirstThread extends Thread{ @Override public void run() { for(int i=0; i<100; i++){ System.out.println("first:"+i); } } }
2、实现Runnable接口
[code]package multithread; public class TestThread02 { public static void main(String[] args) { new TestThread02().begin(); } public void begin(){ MyThread th = new MyThread(); Thread t = new Thread(th); t.start(); for(int i=0; i<100; i++){ System.out.println("first:"+i); } } class MyThread implements Runnable{ @Override public void run() { for(int i=0; i<100; i++){ System.out.println("first:"+i); } } } }
开发中一般采用实现Runnable接口的方式,因为类只能单继承,如果一个类集成了Thread,那么它就不能继承其他类。这在开发中很不灵活。
3、两种方法的变量的不同
[code]package multithread; public class TestThread01 { public static void main(String[] args) { //每个线程都有自己的成员变量空间 Thread th = new FirstThread("mt1"); th.start(); Thread th1 = new FirstThread("mt2"); th1.start(); } } class FirstThread extends Thread{ public FirstThread(String name){ super(name); } private int index = 0; @Override public void run() { for(; index <100; index++){ System.out.println(Thread.currentThread().getName()+":"+index); } } } 结果:每个线程的index都是从0到99 package multithread; public class TestThread02 { public static void main(String[] args) { new TestThread02().begin(); } public void begin(){ //共享mt中的成员变量 MyThread th = new MyThread(); new Thread(th,"mt1").start(); new Thread(th,"mt2").start(); } class MyThread implements Runnable{ private int index = 0; @Override public void run() { for(; index<100; index++){ System.out.println(Thread.currentThread().getName()+":"+index); } } } } 结果:两个线程的index相互增加输出。
启动线程
new Thread(xxx).start();启动线程要用 start 方法
如果使用 run方法,只是简单的执行 run方法而已,没有启动一个线程。
停止线程
1、stop方法 (Deprecated)[code]package multithread; public class TestThreadStop { public static void main(String[] args) { new TestThreadStop().begin(); } public void begin(){ MyThread mt = new MyThread(); Thread tmt = new Thread(mt,"mt1"); tmt.start(); while(true){ if(mt.index==500){ //让线程停止, 该方法已经被淘汰了,因为不安全,不会释放资源,因为线程虽然停止,但是线程里的方法可能还在运行 tmt.stop(); break; } } } class MyThread implements Runnable{ private int index = 0; @Override public void run() { for(; index<1000; index++){ System.out.println("index:"+index); } } } }
2、正确方法
[code]package multithread; public class TestThreadStop { public static void main(String[] args) { new TestThreadStop().begin(); } public void begin(){ MyThread mt = new MyThread(); Thread tmt = new Thread(mt,"mt1"); tmt.start(); while(true){ if(mt.index>2000){ mt.stopThread(); System.out.println("------------"); break; } } } class MyThread implements Runnable{ private int index = 0; private boolean flag = true; @Override public void run() { for(; index<10000; index++){ if(!flag){ break; } System.out.println("index:"+index); } } public void stopThread(){ //在这个位置释放资源 ** 这个很重要 flag = false; } } }
我的计算机是四核的所以模拟不出被停止的现象。
相关文章推荐
- 25、Linux Make 命令详解
- Mac系统快捷键
- lqb 增删改查 练手 记录一下
- Hive面试题—理清hive应用思路
- 在标签的事件属性字符串中编写程序,检查用户输入的密码明文
- Exchange2010配置-多邮箱搜索发现 推荐
- 翻墙用青蛙加速器,上google,youtube,gmail必备
- spring study
- 严澜:搭建高可用的MongoDB集群
- iOS开发:集成微信支付和微信授权登陆
- PBKDF2加密的实现
- chrome 右键页面无反应
- EntityManager的find()与getReference()的区别
- Autodesk应用程序商店下载量达到 一百万!
- WinForm轻松实现自定义分页
- iOS中动态添加属性的方法——关联(e.g. 向Category添加成员变量)
- 文件选择的Filter写法
- IOS6 桌面图标去掉玻璃渲染效果
- Swift IOS url Encoding
- 脚本实现svn备份与恢复,基于rsync实现远程备份,并记录日志