黑马程序员_多线程练习

------- android培训java培训、期待与您交流! ---------- 


一. 售票程序

-------------------------------------------------------------------------------------------------------------------------------------

package thread;

public class TicketDemo {
public static void main(String [] args){
Ticket t=new Ticket();       //3通过Thread类建立线程对象
Thread t1=new Thread(t);    //4.将Runnable接口的子对象作为实际参数传递给Thread类的构造对象
Thread t2=new Thread(t);
t1.start();    //start():开启线程并执行该线程的run方法
t2.start();

}
}

-----------------------------------------------------------------------------------------------------------

lass Ticket implements Runnable{       //1.定义类实现Runnable接口
private  int tick=100;
public void run(){     //2.覆盖Runnable接口中的run方法 ,将线程要运行的代码存放在该run方法中
while(true){
this.show();
}
}
synchronized void show(){
if(tick>0){
    System.out.println(Thread.currentThread().getName()+".....sale"+tick--);
}
}
}

-------------------------------------------------------------------------------------------------------------------------------------



二.生产消费程序

-------------------------------------------------------------------------------------------------------------------------------------

package thread;

public class ProduceConsumerDemo {
public static void main(String[] args) {
      Resource r=new Resource();
      Producer pro=new Producer(r);
      Consumer con=new Consumer(r);
      Thread t1=new Thread(pro);     / /创建四个线程
      Thread t2=new Thread(pro);
      Thread t3=new Thread(con);
      Thread t4=new Thread(con);
   
      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保证每次从头判定 避免信息的覆盖
try {
wait();   //采用等待唤醒机制 以达到一次生产一次消费的目的
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.name=name+"---"+count++;   //标注商品信息
System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
   flag=true ;    //循环时线程等待判定为true 进入wait状态
   this.notifyAll();       //使用norifyAll避免所有线程同时wait的状况
}
public synchronized void out(){
while(!flag){  
try {
wait();
} catch (Exception e) {
e.toString();
}
}
System.out.println(Thread.currentThread().getName()+"...消费者..."+this.name);
   flag=false;
   this.notify();
}
}

-----------------------------------------------------------------------------------------------------------
class Producer implements Runnable{
    private Resource res;
    Producer(Resource res){       //获得Resouce对象
    this.res=res;
    }
public void run() {
while(true){
res.set("--商品--");     / /使用res对象方法
}
}
}

-----------------------------------------------------------------------------------------------------------
class Consumer implements Runnable{   //实现Runnable接口
private Resource res;
Consumer(Resource res){
    this.res=res;
    }
public void run() {
while(true){
res.out();
}
}
}

-------------------------------------------------------------------------------------------------------------------------------------



三.生产消费之JDK1.5新特性

-------------------------------------------------------------------------------------------------------------------------------------

package thread;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ProduceConsumerDemo02 {
public static void main(String[] args) {
Resource02 r=new Resource02();
Producer02 pro=new Producer02(r);
Consumer02 con=new Consumer02(r);
Thread t1=new Thread(pro);   //创建四个线程
Thread t2=new Thread(pro);
Thread t3=new Thread(con);
Thread t4=new Thread(con);  
t1.start();  
t2.start();
t3.start();
t4.start();
}
}

-----------------------------------------------------------------------------------------------------------
class Resource02{
private String name;
private int aa=0;
private boolean flag=false;
private Lock lock=new ReentrantLock();
//Lock实现提供了比使用synchronized方法和语句可获得更广泛的锁定操作
private Condition con_pro=lock.newCondition();
//Condition将Object监视器分解成截然不同的对象,以便于这些对象和Lock实现组合使用
private Condition con_con=lock.newCondition();
public void set(String name) throws InterruptedException{
lock.lock();
try {
while(flag){
con_pro.await();   //调用本线程方法 await()替代wait()
}
this.name=name+"---"+aa++;
System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
   flag=true;
   con_con.signal();        //调用con_con对象方法 signal替代notify()
}finally{
lock.unlock();   
}
}
public  void out() throws InterruptedException{
lock.lock();
try{
while(!flag){
con_con.await();   //对await()采用抛的形式
}
System.out.println(Thread.currentThread().getName()+"...消费者..."+this.name);
   flag=false;
   con_pro.signal();
}finally{
lock.unlock();  //加锁和解锁的动作从隐式到显示  unlock()解说动作必须执行
}
}
}

-----------------------------------------------------------------------------------------------------------
class Producer02 implements Runnable{
    private Resource02 res;
    Producer02(Resource02 res){
    this.res=res;
    }
public void run() {
while(true){
try {
res.set("--商品--");    //set方法有抛出异常  所以要try  catch
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

-----------------------------------------------------------------------------------------------------------
class Consumer02 implements Runnable{
private Resource02 res;
Consumer02(Resource02 res){
    this.res=res;
    }
public void run() {
while(true){
try {
res.out();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}


-----------------------------------------------------------------------------------------------------------

//打印结果:

-----------------------------------------------------------------------------------------------------------

Thread-0...生产者...--商品-----53271
Thread-3...消费者...--商品-----53271
Thread-0...生产者...--商品-----53272
Thread-3...消费者...--商品-----53272
Thread-1...生产者...--商品-----53273
Thread-2...消费者...--商品-----53273
Thread-1...生产者...--商品-----53274
Thread-3...消费者...--商品-----53274
Thread-1...生产者...--商品-----53275
Thread-2...消费者...--商品-----53275
Thread-1...生产者...--商品-----53276
Thread-3...消费者...--商品-----53276
Thread-0...生产者...--商品-----53277
Thread-3...消费者...--商品-----53277
Thread-1...生产者...--商品-----53278
Thread-3...消费者...--商品-----53278
Thread-0...生产者...--商品-----53279
Thread-3...消费者...--商品-----53279
Thread-1...生产者...--商品-----53280
Thread-3...消费者...--商品-----53280
Thread-0...生产者...--商品-----53281
Thread-3...消费者...--商品-----53281
Thread-1...生产者...--商品-----53282
Thread-2...消费者...--商品-----53282
Thread-1...生产者...--商品-----53283
Thread-2...消费者...--商品-----53283
Thread-0...生产者...--商品-----53284
Thread-3...消费者...--商品-----53284

-----------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------------------------------




------- android培训java培训、期待与您交流! ----------

详细请查看:http://edu.csdn.net/heima/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值