生产者消费者模式
在java中,负债产生数据的模块是生产者,负责使用数据的模块是消费者,生产者消费者解决数据的平衡问题,即先有数据后才能使用,没有数据时,消费者需要等待
public class MainBody {
private ArrayList list=new ArrayList();
final int MAX=9;
public synchronized void write(){
while (list.size()>=MAX){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
String value=System.currentTimeMillis()+"填饱你"+new Random().nextInt();
list.add(value);
System.out.println("来菜了");
this.notifyAll();
}
public synchronized void read(){
while (list.size()<MAX){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(list);
list.remove(0);
this.notifyAll();
}
}
public class Client extends Thread{
MainBody mainBody;
public Client (MainBody mainBody){
this.mainBody=mainBody;
}
@Override
public void run() {
while (true){
mainBody.read();
}
}
}
public class Service extends Thread{
MainBody mainBody;
public Service (MainBody mainBody){
this.mainBody=mainBody;
}
@Override
public void run() {
while (true){
mainBody.write();
}
}
}
通过管道实现线程间的通信
在java.io包中的pipestream管道流用于在线程之间传递数据,一个线程发送数据到输出管道,另一个线程从输入管道中读取数据相关的类包括Pipedlnputstream和PipedOutputStream,PipedReader和pipedwriter
public class Threeteem {
public static void main(String[] args) throws IOException {
PipedInputStream inputStream=new PipedInputStream();
PipedOutputStream outputStream=new PipedOutputStream();
inputStream.connect(outputStream);
new Thread(new Runnable() {
@Override
public void run() {
writeDate(outputStream);
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
readData(inputStream);
}
}).start();
}
public static void writeDate(PipedOutputStream outputStream){
try {
for (int i=0;i<100;i++){
String data=""+i;
outputStream.write(data.getBytes());
}
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void readData(PipedInputStream inputStream){
byte[] bytes=new byte[1024];
try {
int len= inputStream.read(bytes);
while (len!=-1){
System.out.println(new String(bytes,0,len));
len=inputStream.read(bytes);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
ThreadLocal是一个为线程访问资源的角度上获取资源出现哄抢出现而设计的类,synchronized就是实现串行来实现的,threadlocal则是为资源问题产生的,如果足够的资源,如果每条线程都有资源就不会发生哄抢的情况,但这个太耗资源,为每条线程提供一个空间,供其存储,threadlocal
static ThreadLocal local=new ThreadLocal();
static class Subthread extends Thread{
@Override
public void run() {
for (int i=0;i<20;i++){
local.set(Thread.currentThread().getName()+"-"+i);
System.out.println(Thread.currentThread().getName()+"---------value"+local.get());
}
}
}
public static void main(String[] args) {
Subthread subthread=new Subthread();
Subthread subthread1=new Subthread();
subthread.start();
subthread1.start();
}
}
它是一个花时间赚空间的类
synchorize是一个内部锁,这个锁很方便,不需要自己去操作,但是有些情况需要自己去操作,这时候就要学习lock
锁的重入性,synchorize是不具备
ReentrantLock的基本使用,它的锁对象可以多次使用
public class OneTest {
private Lock lock= new ReentrantLock();
public class hello extends Thread{
@Override
public void run() {
lock.lock();
for (int i=0;i<20;i++){
System.out.println("helloworld-----"+i+"-----"+Thread.currentThread().getName());
}
lock.unlock();
}
}
public static void main(String[] args) {
OneTest oneTest=new OneTest();
hello h=oneTest.new hello();
hello c=oneTest.new hello();
hello i=oneTest.new hello();
h.start();
c.start();
i.start();
}
}
如果是用lock.lock来锁的话来调用interruptibly根本不会停,除非是使用lock.lockinterruptibly方法,这个方法可以释放锁,但会抛出异常
public class TwoLock {
public static class hello{
static Lock lock=new ReentrantLock();
public void one() throws InterruptedException {
lock.lockInterruptibly();
System.out.println("你好啊世界"+Thread.currentThread().getName());
Thread.sleep(25);
System.out.println("我来了");
lock.unlock();
}
}
public static void main(String[] args) throws InterruptedException {
hello h=new hello();
Thread h1=new Thread(new Runnable() {
@Override
public void run() {
try {
h.one();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread h2=new Thread(new Runnable() {
@Override
public void run() {
try {
h.one();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
h1.start();
h2.start();
h1.join();
h2.interrupt();
}
}
对于synchorized内部锁来说,如果一个线程在等待锁,只有两个结果,要么该线程获得锁继续执行,要么保持等待
对于reentrantlock可重入锁来说,提供另一种可能在等待锁的过程,程序可以根据需要取消对锁的请求
锁的顺序不一致会导致死锁