java多线程(二)
线程的阻塞状态:
参考java多线程(一)多线程的生命周期图解,多线程的五种状态。
1.1 join(),如果在A线程体里面执行了B线程的join()方法,那么A线程阻塞,直到B线程生命周期结束。
1.1.1MyRunnable.java
package com.asiainfo.test.thread5;
/**
* 实现Runnable接口,实现多线程
* @author luke
*
*/
public class MyRunnable implements Runnable {
@Override
public void run() {
for (int i = 0; i < 50; i++) {
System.out.println(Thread.currentThread().getName() + "," + i);
}
}
}
1.1.2 ThreadTest.java
package com.asiainfo.test.thread5;
/**
* 测试类
* @author luke
*
*/
public class ThreadTest {
public static void main(String[] args) {
Runnable runnable = new MyRunnable();
Thread t1 = new Thread(runnable);
for (int i = 0; i < 50; i++) {
System.out.println(Thread.currentThread().getName() + "," + i);
if(i == 30) {
t1.start();
try {
t1.join();//main线程需要等待t1线程结束后,才能继续执行
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
1.2 sleep()线程休眠, 让当前的正在执行的线程暂停指定的时间,并进入阻塞状态。
1.2.1 MyRunnable.java
/**
* 实现Runnable接口,实现多线程
* @author luke
*
*/
public class MyRunnable implements Runnable {
@Override
public void run() {
for (int i = 0; i < 50; i++) {
System.out.println(Thread.currentThread().getName() + "," + i);
}
}
}
1.2.2 ThreadTest.java
package com.asiainfo.test.thread5;
/**
* 测试类
* @author luke
*
*/
public class ThreadTest {
public static void main(String[] args) {
Runnable runnable = new MyRunnable();
Thread t1 = new Thread(runnable);
for (int i = 0; i < 50; i++) {
System.out.println(Thread.currentThread().getName() + "," + i);
if(i == 30) {
t1.start();
try {
t1.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
1.3 yield()线程让步,和线程优先级有关。当某个线程使用了yield()方法从运行状态进入到就绪状态后,cpu只会从选择与该线程同等优先级或者更高优先级的就绪状态的线程去执行。
1.3.1MyThread.java
package com.asiainfo.test.thread6;
/**
* 线程让步yield(),运行中的线程执行了yield()方法后,状态变为(Runnable)就绪状态。
* @author luke
*
*/
public class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("MyThread1" + "," + i);
}
}
}
1.3.2 MyThread2.java
package com.asiainfo.test.thread6;
/**
* 线程让步yield(),运行中的线程执行了yield()方法后,状态变为(Runnable)就绪状态。
* @author luke
*
*/
public class MyThread2 extends Thread {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("MyThread2" + "," + i);
}
}
}
1.3.3 MyThread3.java
package com.asiainfo.test.thread6;
/**
* 线程让步yield(),运行中的线程执行了yield()方法后,状态变为(Runnable)就绪状态。
* @author luke
*
*/
public class MyThread3 extends Thread {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("MyThrea3" + "," + i);
}
}
}
1.3.4 ThreadTest.java
package com.asiainfo.test.thread6;
/**
* 测试类
* yield()线程让步,如果某个线程执行yield(),线程会从运行状态进入
* @author luke
*
*/
public class ThreadTest {
public static void main(String[] args) {
Thread t1 = new MyThread();
Thread t2 = new MyThread2();
Thread t3 = new MyThread3();
for (int j = 0; j < 100; j++) {
System.out.println("main thread j=" + j);
if(j == 20) {
t2.setPriority(3);
t1.setPriority(5);
t3.setPriority(3);
t1.start();
t2.start();
t3.start();
System.out.println("----------yield()before--------");
Thread.yield();
System.out.println("--------yield()after----------");
}
}
}
}
1.4 后台线程(Deamon Thread)
1.4.1 MyThread.java
package com.asiainfo.test.thread5;
/**
* 后台线程(Deamon Thread),后台线程主要为前台线程提服务,如jvm里面的垃圾回收线程。
* Thread的setDeamonThread(true)方法可以把前台线程设置为后台线程;
* 通过isDeamon()可以判断一个线程是否后台线程;
* 当所有的前台线程死亡后,后天线程也会死亡。
* @author luke
*
*/
public class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println("i=" + i);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
1.4.2 ThreadTest.java
package com.asiainfo.test.thread5;
/**
* 测试类
* @author luke
*
*/
public class ThreadTest {
public static void main(String[] args) {
//守护线程
Thread t2 = new MyThread();
for (int j = 0; j < 100; j++) {
System.out.println("main thread j=" + j);
if(j == 20) {
t2.setDaemon(true);//设置线程为后台线程,当前台线程生命周期结束后,后台进程也会结束生命周期。
t2.start();
System.out.println("isDaemon=" + t2.isDaemon());
}
}
}
}
posted on 2017-02-04 02:51 lukelin1989 阅读(155) 评论(0) 编辑 收藏 举报