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 ...
随机推荐
- log4cxx安装和使用
log4cxx是Java社区著名的log4j的c++移植版,用于为C++程序提供日志功能,以便开发者对目标程序进行调试和审计,log4cxx是apache软件基金会的开源项目,基于APR实现跨平台支持 ...
- Lintcode: Subarray Sum Closest
Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first nu ...
- Hibernate Annotation笔记
(1)简介:在过去几年里,Hibernate不断发展,几乎成为Java数据库持久性的事实标准.它非常强大.灵活,而且具备了优异的性能.在本文中,我们将了解如何使用Java 5 注释来简化Hiberna ...
- nyist 676 小明的求助
http://acm.nyist.net/JudgeOnline/problem.php?pid=676 小明的求助 时间限制:2000 ms | 内存限制:65535 KB 难度:2 描述 ...
- 背包九讲 附:USACO中的背包问题
附:USACO中的背包问题 USACO是USA Computing Olympiad的简称,它组织了很多面向全球的计算机竞赛活动. USACO Trainng是一个很适合初学者的题库,我认为它的特色是 ...
- C#:IO
1. File和Directory类 这两个类属于静态类,经常用到的比如File.Exists(string path), Directory.Exists(string path), Directo ...
- php获取文件后缀名格式
function get_extension($file) { substr(strrchr($file, '.'), 1); } 第2种方法: function get_extension($fil ...
- hdu5322 Hope
设dp[n]为n个数字排列时候的答案,那么可以得到dp方程 dp[n]=Σdp[n-i]*c(n-1,i-1)*(i-1)!*i^2(1<=i<=n) 然后上式可以化成卷积形式,分治FFT ...
- windows系统调用 线程创建
#include "windows.h" #include "iostream" using namespace std; class CWorkerThrea ...
- 转:装完Centos7提示Initial setup of CentOS Linux 7 (core)
在用U盘装完CentOS后,重新开机启动后显示: Initial setup of CentOS Linux 7 (core) 1) [x] Creat user 2) [!] License inf ...