JAVA基础知识之多线程——控制线程

Posted 云中之歌

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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基础之多线程

Java基础之多线程

java基础之多线程二:多线程实现方式

java基础之多线程知识懂多少

Java之多线程

python基础之多线程