yield()表示線程謙讓,將釋放cpu;
原子基本對象類可以優化線程,基本類型的加減操作可能出現併發,需要加鎖;這時使用automic類;
lock() ;unlock()函數與syncnozied,都是同步代碼塊,不同在於,lock()同步多個函數操作;
ThreadLocal 變量是線程複製創建的,獨有的,不會被其他線程修改,最好做出static 類類型;
static ThreadLocal<Integer> threadInt;
suspend()和resume()被廢止,可能導致死鎖;
stop()方法廢止,因爲不釋放鎖;
Thread.interrupt()終止任務,一般是阻塞的線程;會拋出異常;Thread.interrupted()不會拋出異常;
任務之間的協作。Condition類的await();signal()和signalAll()函數;Object類的wait(),notify()
無界隊列ListedBlockingQueue,同步隊列BlockingQueue,有界隊列ArrayBlockingQueue;生產者和消費者隊列;
未完
簡單的消費者,生產者線程
public class Sig {
public static void main(String[] args)
{
/* PipedWriter out=new PipedWriter();
PipedReader reader=new PipedReader();*/
LinkedList<Integer> list = new LinkedList<>();
Producer p = new Producer(list, 10);
Consumer c1 = new Consumer(list);
Consumer c2 = new Consumer(list);
Thread producer = new Thread(p);
producer.setName("生產者線程");
Thread consumer1 = new Thread(c1);
consumer1.setName("消費者1");
Thread consumer2 = new Thread(c2);
consumer2.setName("消費者2");
producer.start();
consumer1.start();
consumer2.start();
}
}
class Producer implements Runnable
{
private LinkedList<Integer> list;
private final int maxSize;
public Producer(LinkedList list, int size)
{
this.list = list;
maxSize =size;
}
@Override
public void run()
{
try
{
while(true)
{
//模擬耗時1s
Thread.sleep(10);
synchronized (list)
{
if(list.size()==maxSize)
{
System.out.println("緩衝區已滿,正在等待消費者消費..." + System.currentTimeMillis());
list.wait();
}
else
{
list.add(produce());
list.notifyAll();
}
}
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
private int produce()
{
int n = new Random().nextInt(100);
System.out.println("Thread: " + Thread.currentThread().getName() + " produce: " + n);
return n;
}
}
class Consumer implements Runnable
{
private LinkedList<Integer> list;
public Consumer(LinkedList list)
{
this.list = list;
}
@Override
public void run()
{
while (true)
{
try
{
synchronized(list)
{
//模擬耗時
Thread.sleep(10);
if(list.isEmpty())
{
System.out.println("緩衝區已空,正在等待生產者生產..." + System.currentTimeMillis() + Thread.currentThread().getName());
list.wait();
}
else
{
consume(list.poll());
list.notifyAll();
}
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
private void consume(Integer n)
{
System.out.println("Thread:" + Thread.currentThread().getName() + " consume: " + n);
}
}
簡單的lock信號量鎖,控制函數的執行順序
public class ThreeConditionCommunication
{
/**
* 用於標記那個線程進行執行
* 1:主線程
* 2:線程2
* 3:線程3
*/
private static int HASOUT = 1;
public static void main(String[] args) throws InterruptedException
{
final Core core = new Core();
//子線程2
new Thread(
new Runnable()
{
@Override
public void run()
{
for (int i = 1; i <= 5; i++)
{
core.SubMethod2(i);
}
}
}
).start();
//子線程3
new Thread(
new Runnable()
{
@Override
public void run()
{
for (int i = 1; i <= 5; i++)
{
core.SubMethod3(i);
}
}
}
).start();
//主線程
for (int i = 1; i <= 5; i++)
{
core.MainMethod(i);
}
}
/**
* 創建一個靜態的類
* 將核心的業務邏輯的方法放在這裏
* 體現了高內聚的特點
* @author huang.jf
*/
static class Core{
//創建一個Lock鎖對象
public Lock lock = new ReentrantLock();
//創建三個Condition對象 分別用於控制三個線程
public Condition condition1 = lock.newCondition();
/* public Condition condition2 = lock.newCondition();
public Condition condition3 = lock.newCondition();*/
//線程2 循環輸出10次
public void SubMethod2(int j){
lock.lock();//開啓Lock鎖
try{
//true 執行
//false 等待
while(HASOUT!=2){
try
{
condition1.await();//線程2等待
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
//線程2執行
System.out.println("this is sub2 thread...");
HASOUT = 3;
condition1.signalAll();//喚醒線程3
}finally{
lock.unlock();//釋放鎖
}
}
//線程3 循環輸出20次
public void SubMethod3(int j){
lock.lock();//開啓Lock鎖
try{
//true 執行
//false 等待
while(HASOUT!=3){
try
{
condition1.await();//線程3等待
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
//線程3執行
System.out.println("this is sub3 thread...");
HASOUT = 1;
condition1.signalAll();//喚醒線程1(主線程)
}finally{
lock.unlock();//釋放鎖
}
}
//主線程調用循環輸出一百次
public void MainMethod(int j){
lock.lock();
try{
//false 執行
//true 等待
while(HASOUT!=1){
try
{
condition1.await();//主線程等待
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
//執行主線程
System.out.println("this is main thread...");
HASOUT = 2;
condition1.signalAll();//喚醒線程2
}finally{
lock.unlock();
}
}
}
}