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();
}
}
}
}