JAVA基础知识之多线程——控制线程
join线程
在某个线程中调用其他线程的join()方法,就会使当前线程进入阻塞状态,直到被join线程执行完为止。join方法类似于wait, 通常会在主线程中调用别的线程的join方法,这样可以保证在所有的子线程执行结束之后在主线程中完成一些统一的步骤。
下面是一个例子,
- package threads;
- public class JoinThread extends Thread {
- public JoinThread(String name) {
- super(name);
- }
- public void run() {
- for(int i=0; i<20; i++) {
- System.out.println(getName()+" "+i);
- }
- }
- public static void main(String[] args) throws InterruptedException {
- new JoinThread("new Thread-1").start();
- for (int i=0; i<50; i++) {
- if(i==20){
- JoinThread jt = new JoinThread("new thread-2");
- jt.start();
- //main线程调用了jt线程的join方法,main线程
- //必须等jt执行完之后才能继续执行
- jt.join();
- }
- System.out.println(Thread.currentThread().getName()+" "+i);
- }
- }
- }
执行结果,可见当主线程中i=20时,主线程被阻塞了,测试thread-1和thread-2正在执行,一直到thread-2执行结束之后,主线程才继续执行
- main 0
- new Thread-1 0
- main 1
- new Thread-1 1
- main 2
- new Thread-1 2
- main 3
- new Thread-1 3
- main 4
- new Thread-1 4
- main 5
- new Thread-1 5
- main 6
- new Thread-1 6
- main 7
- new Thread-1 7
- main 8
- new Thread-1 8
- main 9
- new Thread-1 9
- main 10
- new Thread-1 10
- main 11
- new Thread-1 11
- main 12
- new Thread-1 12
- main 13
- new Thread-1 13
- main 14
- new Thread-1 14
- main 15
- new Thread-1 15
- main 16
- new Thread-1 16
- main 17
- new Thread-1 17
- main 18
- new Thread-1 18
- main 19
- new Thread-1 19
- new thread-2 0
- new thread-2 1
- new thread-2 2
- new thread-2 3
- new thread-2 4
- new thread-2 5
- new thread-2 6
- new thread-2 7
- new thread-2 8
- new thread-2 9
- new thread-2 10
- new thread-2 11
- new thread-2 12
- new thread-2 13
- new thread-2 14
- new thread-2 15
- new thread-2 16
- new thread-2 17
- new thread-2 18
- new thread-2 19
- main 20
- main 21
- main 22
- main 23
- main 24
- main 25
- main 26
- main 27
- main 28
- main 29
- main 30
- main 31
- main 32
- main 33
- main 34
- main 35
- main 36
- main 37
- main 38
- main 39
- main 40
- main 41
- main 42
- main 43
- main 44
- main 45
- main 46
- main 47
- main 48
- main 49
后台线程:setDaemon
后台线程有个特征就是,如果所有前台线程都死亡,后台线程会自动死亡。JVM的垃圾回收器就是一种典型的后台线程。Thread提供了一个isDaemon方法判断是否为后台线程。
下面是一个例子,注意必须在线程start之前设置(setDaemon(true))为后台线程。
- package threads;
- public class DaemonThread extends Thread {
- public void run() {
- for(int i=0; i<1000; i++) {
- System.out.println(getName()+" "+i);
- }
- }
- public static void main(String[] args) {
- DaemonThread t = new DaemonThread();
- //必须在start之前设置成后台线程
- t.setDaemon(true);
- t.start();
- for (int i=0; i<3; i++) {
- System.out.println(Thread.currentThread().getName()+" "+i);
- }
- //main进程运行到此处时结束,后台进程也会随之结束
- }
- }
执行结果,可以看到子线程的i并没有累加到1000就结束了,因为子线程被设置成了后台线程,主线程先于子线程结束,子线程也就随之结束了
- main 0
- Thread-5 0
- main 1
- Thread-5 1
- main 2
- Thread-5 2
- Thread-5 3
- Thread-5 4
- ...
- Thread-5 199
- Thread-5 200
- Thread-5 201
- Thread-5 202
线程睡眠:sleep
sleep数Thread类的一个静态方法,让当前线程进入阻塞状态。
在sleep指定的时间到达之前,线程都不会获得执行机会,即使有可用的CPU执行片,因此sleep方法常常用来暂停线程。
sleep方法需要抛出一个异常。
下面是一个例子,
- package threads;
- import java.util.Date;
- public class SleepThread {
- public static void main(String[] args) throws InterruptedException {
- for (int i=0; i<10; i++) {
- System.out.println("time:" + new Date());
- Thread.sleep(1000);
- }
- }
- }
执行结果,
- time:Wed Nov 16 12:00:58 CST 2016
- time:Wed Nov 16 12:00:59 CST 2016
- time:Wed Nov 16 12:01:00 CST 2016
- time:Wed Nov 16 12:01:01 CST 2016
- time:Wed Nov 16 12:01:02 CST 2016
- time:Wed Nov 16 12:01:03 CST 2016
- time:Wed Nov 16 12:01:04 CST 2016
- time:Wed Nov 16 12:01:05 CST 2016
- time:Wed Nov 16 12:01:06 CST 2016
- time:Wed Nov 16 12:01:07 CST 2016
线程优先级 及 线程让步:yield
优先级
JAVA 使用setPriority改变线程优先级。JVM提供了三个常量可在不同平台使用,分别是MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY,当然也可以使用1。。10之类的数字,但是不同平台定义的数字不同,所以还是推荐用常量。
yield
yield也是Thread类的一个静态方法,它和sleep有些类似,都可以暂停当前线程,不同的是它不会让线程进入阻塞状态,而是直接进入就绪状态,让当前线程重新进入资源竞争中,此时只有优先级等于或者高于当前线程的其他线程才能获取CPU资源执行,否则被yield的线程将重新获取资源继续执行。
下面是一个例子,
- package threads;
- public class YieldThread extends Thread{
- public YieldThread(String name) {
- super(name);
- }
- public void run() {
- for ( int i = 0; i<50; i++) {
- System.out.println(getName()+" "+i);
- if (i == 20) {
- Thread.yield();
- }
- }
- }
- public static void main(String[] args) throws InterruptedException {
- YieldThread yt1 = new YieldThread("thread-1");
- yt1.setPriority(Thread.MAX_PRIORITY);
- //yt1.setPriority(7);
- yt1.start();
- Thread.sleep(1);
- YieldThread yt2 = new YieldThread("thead-2 ");
- yt2.setPriority(Thread.MIN_PRIORITY);
- //yt2.setPriority(1);
- yt2.start();
- }
- }
执行结果,可以看到,因为thread-1优先级比thread-2高,所以无轮是thread-1调用了yield还是thread-2调用了yield,都是thread-1先抢到资源继续执行
- thread-1 0
- thread-1 1
- thread-1 2
- thread-1 3
- thread-1 4
- thread-1 5
- thread-1 6
- thread-1 7
- thread-1 8
- thread-1 9
- thread-1 10
- thread-1 11
- thread-1 12
- thread-1 13
- thread-1 14
- thread-1 15
- thread-1 16
- thread-1 17
- thread-1 18
- thread-1 19
- thread-1 20
- thread-1 21
- thread-1 22
- thread-1 23
- thead-2 0
- thread-1 24
- thead-2 1
- thread-1 25
- thead-2 2
- thread-1 26
- thead-2 3
- thread-1 27
- thead-2 4
- thread-1 28
- thead-2 5
- thread-1 29
- thead-2 6
- thread-1 30
- thead-2 7
- thread-1 31
- thead-2 8
- thread-1 32
- thead-2 9
- thread-1 33
- thead-2 10
- thread-1 34
- thead-2 11
- thread-1 35
- thead-2 12
- thread-1 36
- thead-2 13
- thread-1 37
- thead-2 14
- thread-1 38
- thead-2 15
- thread-1 39
- thead-2 16
- thread-1 40
- thead-2 17
- thread-1 41
- thead-2 18
- thread-1 42
- thead-2 19
- thread-1 43
- thead-2 20
- thread-1 44
- thead-2 21
- thead-2 22
- thread-1 45
- thead-2 23
- thread-1 46
- thead-2 24
- thead-2 25
- thread-1 47
- thead-2 26
- thread-1 48
- thead-2 27
- thread-1 49
- thead-2 28
- thead-2 29
- thead-2 30
- thead-2 31
- thead-2 32
- thead-2 33
- thead-2 34
- thead-2 35
- thead-2 36
- thead-2 37
- thead-2 38
- thead-2 39
- thead-2 40
- thead-2 41
- thead-2 42
- thead-2 43
- thead-2 44
- thead-2 45
- thead-2 46
- thead-2 47
- thead-2 48
- thead-2 49
JAVA基础知识之多线程——控制线程的更多相关文章
- Java基础知识(多线程和线程池)
新建状态: 一个新产生的线程从新状态开始了它的生命周期.它保持这个状态直到程序 start 这个线程. 运行状态:当一个新状态的线程被 start 以后,线程就变成可运行状态,一个线程在此状态下被认为 ...
- JAVA基础知识之多线程——线程组和未处理异常
线程组 Java中的ThreadGroup类表示线程组,在创建新线程时,可以通过构造函数Thread(group...)来指定线程组. 线程组具有以下特征 如果没有显式指定线程组,则新线程属于默认线程 ...
- JAVA基础知识之多线程——线程通信
传统的线程通信 Object提供了三个方法wait(), notify(), notifyAll()在线程之间进行通信,以此来解决线程间执行顺序等问题. wait():释放当前线程的同步监视控制器,并 ...
- JAVA基础知识之多线程——线程池
线程池概念 操作系统或者JVM创建一个线程以及销毁一个线程都需要消耗CPU资源,如果创建或者销毁线程的消耗源远远小于执行一个线程的消耗,则可以忽略不计,但是基本相等或者大于执行线程的消耗,而且需要创建 ...
- java基础知识总结--多线程
1.扩展Java.lang.Thread类 1.1.进程和线程的区别: 进程:每个进程都有自己独立的代码和数据空间(进程上下文),进程间的切换会有较大的开销,一个进程包含1~n个线程. 线程:同一类线 ...
- Java基础加强之多线程篇(线程创建与终止、互斥、通信、本地变量)
线程创建与终止 线程创建 Thread类与Runnable接口的关系 public interface Runnable { public abstract void run(); } public ...
- JAVA基础知识之多线程——三种实现多线程的方法及区别
所有JAVA线程都必须是Thread或其子类的实例. 继承Thread类创建线程 步骤如下, 定义Thead子类并实现run()方法,run()是线程执行体 创建此子类实例对象,即创建了线程对象 调用 ...
- JAVA基础知识系列---进程、线程安全
1 相关概念 1.1 临界区 保证在某一时刻只有一个线程能访问数据的简便方法,在任意时刻只允许一个线程对资源进行访问.如果有多个线程试图同时访问临界区,那么在有一个线程进入后,其他所有试图访问临界区的 ...
- JAVA基础知识之多线程——线程同步
线程安全问题 多个线程同时访问同一资源的时候有可能会出现信息不一致的情况,这是线程安全问题,下面是一个例子, Account.class , 定义一个Account模型 package threads ...
随机推荐
- CSS文档流与块级元素和内联元素
CSS文档流与块级元素(block).内联元素(inline),之前翻阅不少书籍,看过不少文章, 看到所多的是零碎的CSS布局基本知识,比较表面.看过O'Reilly的<CSS权威指南>, ...
- Swift游戏实战-跑酷熊猫 11 欢迎进入物理世界
物理模拟是一个奇妙的事情,以此著名的游戏有愤怒的小鸟.我们在这节将会一起来了解如何设置重力,设置物理包围体,碰撞的检测. 要点: 设置物理检测的代理: 让主场景遵循SKPhysicsContactDe ...
- window.cookie
本地测试cookie用火狐来测试 首先cookie是document上的一个属性. 先弹出一个cookie alert(document.cookie); //弹出是空的 设置cookie,格式是有一 ...
- navicat的简单应用
首先 创建连接 主机名 : 可以不写名称随意 主机名/IP地址:localhost或者127.0.0.1 都是本机的意思 端口:默认3306 尽量不要改怕与其余端口重复,如有重名端口系统会报错 ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest Lunch Time
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5499 The 12th Zhejiang Provincial ...
- android设置系统模式
android 静音与振动1,设置静音和振动静音和振动都属于来电后的动作.所以在设置静音和振动时都只是设置一些标识,并往数据库写入相应标识. 文件:packages/apps/settings/src ...
- 实现listview的条目点击后改变背景颜色
gv_categoryeffect_gridview.setChoiceMode(GridView.CHOICE_MODE_SINGLE);,再设置一个selector的背景选择器 getResour ...
- paper 43 :ENDNOTE下载及使用方法简介
转载来源:http://blog.sciencenet.cn/blog-484734-367968.html 软件下载来源: EndNote v9.0 Final 正式版:http://www.ttd ...
- JQuery获取和设置Select选项常用方法总结
1.获取select 选中的 text: $("#cusChildTypeId").find("option:selected").text(); $(&quo ...
- 对linux的根目录执行强制递归移除
开始开始时使用: #rm -f -r / 提示对根目录使用递归操作很危险,然后就没执行成功,让使用 --no-preserve-root 这个参数. 好吧,反正是虚拟机 于是执行: #rm -f -r ...