lock
import java.util.concurrent.locks.*;
/*lock*/
class ProducerConsumerDemo2 {
public static void main(String[] args) {
Resource r = new Resource();
Producer p = new Producer(r);
Consumer c =new Consumer(r);
Thread t1 = new Thread(p);
Thread t2 = new Thread(p);
Thread t3 = new Thread(c);
Thread t4 = new Thread(c);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Resource {
private String name;
private int count = 1;
private boolean flag = false;
private Lock lock = new ReentrantLock();
private Condition ct_1 = lock.newCondition();
private Condition ct_2 = lock.newCondition();
public void set(String name) throws InterruptedException {
lock.lock();
try {
while(flag)
ct_1.await();
this.name = name+"....."+count++;
System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
flag = true;
ct_2.signal();
}
finally {
lock.unlock();
}
}
public void out() throws InterruptedException {
lock.lock();
try {
while(!flag)
ct_2.await();
System.out.println(Thread.currentThread().getName()+"...消费者..........."+this.name);
flag = false;
ct_1.signal();
}
finally {
lock.unlock();
}
}
}
class Producer implements Runnable {
private Resource res;
Producer(Resource res) {
this.res = res;
}
public void run() {
while(true) {
try {
res.set(">>西瓜<<");
}
catch (InterruptedException i) {}
}
}
}
class Consumer implements Runnable {
private Resource res;
Consumer(Resource res) {
this.res = res;
}
public void run() {
while(true) {
try {
res.out();
}
catch (InterruptedException i) {}
}
}
}
生产者 消费者
/*生产者消费者*/
class ProducerConsumerDemo {
public static void main(String[] args) {
Resource r = new Resource();
Producer p = new Producer(r);
Consumer c =new Consumer(r);
Thread t1 = new Thread(p);
Thread t2 = new Thread(p);
Thread t3 = new Thread(c);
Thread t4 = new Thread(c);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Resource {
private String name;
private int count = 1;
private boolean flag = false;
public synchronized void set(String name) {
while(flag) { //用while 比 if 的好处在于,在已经在下一步 wait中的线程也会回来判断真假
try {
this.wait();
}
catch(Exception e) {}
}
this.name = name+"....."+count++;
System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
flag = true;
this.notifyAll(); //全部唤醒
}
public synchronized void out() {
while(!flag) {
try {
this.wait();
}
catch(Exception e) {}
}
System.out.println(Thread.currentThread().getName()+"...消费者..........."+this.name);
flag = false;
this.notifyAll();
}
}
class Producer implements Runnable {
private Resource res;
Producer(Resource res) {
this.res = res;
}
public void run() {
while(true) {
res.set(">>西瓜<<");
}
}
}
class Consumer implements Runnable {
private Resource res;
Consumer(Resource res) {
this.res = res;
}
public void run() {
while(true) {
res.out();
}
}
}
中断 守护线程
/*中断 守护线程*/
class StopThreadDemo2 {
public static void main(String[] args) {
StopThread st = new StopThread();
Thread t1 = new Thread(st);
Thread t2 = new Thread(st);
t1.setDaemon(true); //守护线程 在其他线程结束后 会自动结束 java虚拟机关闭
t2.setDaemon(true);
t1.start();
t2.start();
int num = 0;
while(true) {
if(num++ == 60) {
// st.changeFlag();
// t1.interrupt(); //对冻结进行清除 恢复运行状态
// t2.interrupt();
break;
}
System.out.println(Thread.currentThread().getName()+"...man"+num);
}
}
}
class StopThread implements Runnable {
private boolean flag = true;
public synchronized void run() {
while(flag) {
try {
wait();
}
catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName()+"...Exception");
flag = false;
}
System.out.println(Thread.currentThread().getName()+"...run");
}
}
public void changeFlag() {
flag = false;
}
}
优先级 join方法
/*
join 方法
获得cpu执行权
优先级
所有线程默认优先级是5 级别是 1到10
setPrority()方法设置优先级
yield() 方法
*/
class Demo implements Runnable {
public void run() {
for(int x=0 ; x<60 ; x++) {
System.out.println(Thread.currentThread().toString()+"....."+x);
Thread.yield(); //临时释放执行权
}
}
}
class JoinDemo {
public static void main(String[] args) throws Exception {
Demo d = new Demo();
Thread t1 = new Thread (d);
Thread t2 = new Thread (d);
t1.start();
//t1.join(); //
// t1.setPriority(Thread.MAX_PRIORITY); //设置最高优先级
t2.start();
for(int x=0; x<70 ; x++) {
System.out.println("main....."+x);
}
System.out.println("over");
}
}