码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA线程

时间:2017-07-02 12:24:09      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:main   interrupt   ring   font   sso   system   jsb   pac   rac   

技术分享

线程的各种状态如上图所看到的。

对于wait/notify的測试,我将会留到 生产者消费者模式中实现。

对于join、interrupt的測试例如以下:

package com.huan;

public class ThreadTest {
	
	public static void main(String[] args) throws Exception{
//		joinTest();
		interruptTest();
	}
	
	public static void joinTest(){
		new Thread(){
			@Override
			public void run() {
				Thread t1 = new Thread(){
					@Override
					public void run() {
						try {
							Thread.sleep(3000);
							System.out.println("//t1 thread");
						} catch (InterruptedException e) {
							System.out.println("//sleep interrupted");
						}

					}

				};
				t1.start();
				try {
					t1.join();
				} catch (InterruptedException e) {
					System.out.println("//join interrupted");
				}
				System.out.println("//out thread");
			}
		}.start();
		
		//t1 thread
		//out thread
	};
	
	public static void interruptTest(){
		new Thread(){
			@Override
			public void run() {
				Thread t1 = new Thread(){
					@Override
					public void run() {
						try {
							Thread.sleep(3000);
							System.out.println("//t1 thread");
						} catch (InterruptedException e) {
							System.out.println("//sleep interrupted");
						}

					}

				};
				t1.start();
				System.out.println("//out thread");
				t1.interrupt();
			}
		}.start();
		//out thread
		//sleep exception
	};
	
}

JAVA线程

标签:main   interrupt   ring   font   sso   system   jsb   pac   rac   

原文地址:http://www.cnblogs.com/slgkaifa/p/7105527.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!