java线程笔记

最简单的调用例子

package thread.test;

public class Test01 {

	public static void main(String[] args) {
		Thread th1 = new ThreadExtend();	//继承时的用法
		Thread th2 = new Thread(new ThreadImplement());	//接口时的用法
		th1.start();
		th2.start();
	}

}

class ThreadExtend extends Thread{
	public void run() {
		System.out.println(111);
	}
}

class ThreadImplement implements Runnable{
	public void run() {
		System.out.println(222);
	}
}

阻塞

当一个线程将一个对象锁住时,其他线程需要等待锁释放后才可以同步访问这个对象。

package thread.test;

public class ThreadTestLock {

	public static Integer STATIC_INT = 0;
	
	public static void main(String[] args) {
		new LockTest1().start();
		new LockTest2().start();
	}
	
}

class LockTest1 extends Thread{
	public void run() {
		
		synchronized (ThreadTestLock.STATIC_INT) {
			System.out.println("LockTest1 start");
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			ThreadTestLock.STATIC_INT++;
			System.out.println("LockTest1 end");
		}
	}
}

class LockTest2 extends Thread{
	public void run() {
		
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		synchronized (ThreadTestLock.STATIC_INT) {
			System.out.println("LockTest2 start");
			ThreadTestLock.STATIC_INT++;
			System.out.println("LockTest2 end");
		}
		
	}
}

控制台输出

LockTest1 start
LockTest1 end
LockTest2 start
LockTest2 end

为了确保LockTest1 先执行,我在LockTest2 中先等待了0.5秒,之后LockTest1 会将STATIC_INT 锁住5秒。在这5秒中,LockTest2 陷入阻塞,必须在5秒后才会继续执行。

wait,sleep,join之类的方法也会显式的造成阻塞,但是,这种主动定义的阻塞没有必要多说。

死锁

被锁住的对象必须要在锁释放后才能同步访问,但是由于一些原因,锁永远不会被释放的话,就会产生死锁。
一个简单的例子:两个线程在锁住自有对象的同时,同步的去访问对方所持有的对象。

public class ThreadTestDeathLock {

	public static Integer INT1 = 1;
	public static Integer INT2 = 2;
	
	public static void main(String[] args) {
		new DeathLock1().start();
		new DeathLock2().start();
	}

}

class DeathLock1 extends Thread{
	public void run() {
		
		try {
			synchronized (ThreadTestDeathLock.INT1) {
				System.out.println("int1 locked");
				Thread.sleep(2000);
				synchronized (ThreadTestDeathLock.INT2) {
					
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		
	}
}

class DeathLock2 extends Thread{
	public void run() {
		try {
			synchronized (ThreadTestDeathLock.INT2) {
				System.out.println("int2 locked");
				Thread.sleep(2000);
				synchronized (ThreadTestDeathLock.INT1) {
					
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}
}

wait用法

基本语法

	public static void main(String[] args) throws InterruptedException {
		synchronized ("") {
			"".wait();
		}
	}

同步锁的对象与调用wait的对象必须是同一个,否则会报错

	public static void main(String[] args) throws InterruptedException {
		synchronized ("") {
			"1".wait();
		}
	}

比如上面的写法会报java.lang.IllegalMonitorStateException

另一种写法

	public synchronized void aaa() {
		try {
			this.wait();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

synchronized关键字加在方法上时,锁住的对象实际为this,因此下面也是用this来调用wait的。
synchronized加在类上时,同理。

重载

wait有三种重载,wait(long),wait(long, int),wait()。
wait(long)输入等待的毫秒数,wait(long, int)输入额外的纳秒数,就是字面意思,不做解释。
wait()执行后会一直等待,直到当前对象被notify。

notify

被notify的对象会解除wait状态。

public class ThreadWaitNotifyTest extends Thread{

	public static void main(String[] args) throws InterruptedException {
		ThreadWaitNotifyTest test = new ThreadWaitNotifyTest();
		test.start();
		Thread.sleep(500);
		synchronized (test) {
			System.out.println("n st");
			test.notifyAll();
			System.out.println("n ed");
		}
		
	}
	
	public void run() {
		System.out.println("aaa st");
		synchronized (this) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println("aaa ed");
	}

}

输出

aaa st
n st
n ed
aaa ed

而不是输出

aaa st
aaa ed
n st
n ed

即可证明notify的作用。

线程池

网上例子很多,这里只给出一个简单样例

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		ThreadPoolExecutor executor = 
				new ThreadPoolExecutor(5, 10, 10000, TimeUnit.MICROSECONDS, new ArrayBlockingQueue<Runnable>(5));
		
		for(int i = 0; i < 15; i++){
			MyTask myTask = new MyTask(i);
			executor.execute(myTask);
			System.out.println("pool size = " + executor.getPoolSize() + " queue size = " + executor.getQueue().size() + 
					"completed size = " + executor.getCompletedTaskCount());
		}
		
		executor.shutdown();
		
	}

}


class MyTask implements Runnable{
	
	private int taskNum;
	
	public MyTask(int taskNum) {
		// TODO Auto-generated constructor stub
		this.taskNum = taskNum;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		System.out.println("start task " + this.taskNum + " start");
		
		try {
			Thread.currentThread().sleep(4000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("catch");
		}
		
		System.out.println("end task " + this.taskNum + " end");
		
	}
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值