一个生产者 和 一个消费者dom
public class ProduceandCustomer2 {ide
public static void main(String[] args) {
Produce2 p = new Produce2();
Customer2 c = new Customer2();
Thread t1 = new Thread(p);
Thread t2 = new Thread(c);
t1.setName("生产者");
t2.setName("消费者");
t1.start();
t2.start();
}
}线程
class BaoZi {
public static boolean flag; //false 为没有包子 true 为有包子
public static Object obj = new Object();
}对象
class Produce2 implements Runnable{内存
@Override
public void run() {
synchronized (BaoZi.obj) {
while(true){
if(BaoZi.flag){
//有包子时
try {
BaoZi.obj.wait();//生产者等待
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 没包子的时候
BaoZi.flag = true; //生产者生产
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "生产了一个包子");
BaoZi.obj.notify();//唤醒 生产者
}
}
}
}
//消费者
class Customer2 implements Runnable{rem
@Override
public void run() {
synchronized (BaoZi.obj) {
while(true){
if(!BaoZi.flag){
//无包子时
try {
BaoZi.obj.wait();//消费者等待
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//有包子时
BaoZi.flag = false; //消费者消费包子
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "消费了一个包子");
BaoZi.obj.notify();//唤醒 消费者
}
}
}
}get
----------------------------同步
静态 同步方法的 锁it
public class StaticThread {io
public static void main(String[] args) {
}
}
class MyThre implements Runnable{
private static int sum = 0;
public boolean flag = true;
@Override
public void run() {
if(flag){
for(int i = 0;i<3;i++){
//锁 MyThre.class 为当前类 的字节码
synchronized (MyThre.class) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sum +=100;
System.out.println(Thread.currentThread().getName() + ":" + sum);
flag = false;
}
}
}else{
for(int i = 0;i<3;i++){
try {
Thread.sleep(8);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setrun();
}
}
}
//静态同步方法 锁为 MyThre.class
static public void setrun(){
sum +=100;
System.out.println(Thread.currentThread().getName() + "同步方法:" + sum);
}
-------------------------------------------
Lock 的使用 和用法
public class Lock1 {
public static void main(String[] Baozirgs) {
Pr p1 = new Pr();
Cus c1 = new Cus();
p1.setName("生产者1");
c1.setName("消费者1");
Pr p2 = new Pr();
Cus c2 = new Cus();
p2.setName("生产者2");
c2.setName("消费者2");
p1.start();
c1.start();
p2.start();
c2.start();
}
}
class Baozi {
public static int id ;
public static List<Baozi> list = new ArrayList<Baozi>();
//Lock 对象的建立
public static Lock lock = new ReentrantLock();
//生产者 和消费者 的监视
public static Condition pr = lock.newCondition();
public static Condition cus = lock.newCondition();
}
class Pr extends Thread{
@Override
public void run() {
while(true){
Baozi.lock.lock();
while(Baozi.list.size() == 6){
try {
//等待
Baozi.pr.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
}
Baozi baozi = new Baozi();
baozi.id = Baozi.id++;
Baozi.list.add(baozi);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()
+ "生产了id为:" + baozi.id
+ "剩余包子:" + Baozi.list.size());
//唤醒消费者
Baozi.cus.signal();
//释放锁
Baozi.lock.unlock();
}
}
}
class Cus extends Thread{
@Override
public void run() {
while(true){
Baozi.lock.lock();
while(Baozi.list.size() == 0){
try {
//消费者 等待
Baozi.cus.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
int index =(int)Math.random()*Baozi.list.size();
Baozi baozi2 = Baozi.list.remove(index);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()
+ "消费了包子id:" + baozi2.id
+ "现有包子" + Baozi.list.size());
//唤醒 生产者
Baozi.pr.signal();
//释放锁
Baozi.lock.unlock();
}
}
-----------------------------------------
线程池 --> 由于线程的建立 须要时间 而线程池 就是为了解决线程建立须要花费
不少 建立的时间 而致使程序运行效率慢 的这个问题
线程池的建立 和使用
//建立一个线程池对象
//线程池里有3个线程
ExecutorService service = Executors.newFixedThreadPool(3);
service.execute(new Runnable() {
@Override
public void run(){
for(int i = 0;i<100;i++){
system.out.println(i);
}
}
});
service.shudown();//销毁完成 任务的线程 service = null; //将完成任务的线程 占用的内存 回收释放