一個生產者 和 一個消費者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; //將完成任務的線程 佔用的內存 回收釋放