首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

拥塞队列实例-《Java编程思想》并发习题29

2012-12-24 
阻塞队列实例-《Java编程思想》并发习题29花了一个半晚上,终于独立搞定这道阻塞队列的习题,虽然对高手可能是

阻塞队列实例-《Java编程思想》并发习题29
花了一个半晚上,终于独立搞定这道阻塞队列的习题,虽然对高手可能是小kiss,但对于我这种新手还是蛮开心的。

我准备把《Java编程思想》学习完之后就去换一份Java开发类的工作,本人机械专业,有高手路过还请指点!

处理并发程序最关键的是确定互斥量并良好的处理各线程之间的资源共享。
-----------------------------
package concurrency;
/*Exercise 29:   (8) Modify ToastOMatic.java to create peanut butter and jelly on toast
sandwiches using two separate assembly lines (one for peanut butter, the second for jelly,
then merging the two lines).  */
import java.util.concurrent.*;
import java.util.*;
import static net.mindview.util.Print.*;

class ToastQue extends LinkedBlockingQueue<Toast29> {}
class SandwichQue extends LinkedBlockingQueue<Sandwich> {}
//define Toast29;
class Toast29 {
  public enum Status { DRY, PEANUT, JELLY}
  private Status status = Status.DRY;
  private final int id;
  public Toast29(int idn) { id = idn; }
  public void peanut() { status = Status.PEANUT; }
  public void jelly(){status=Status.JELLY;}
  public Status getStatus() { return status; }
  public int getId() { return id; }
  public String toString() {
    return "Toast " + id + ": " + status;
  }
}


class Toaster29 implements Runnable {
  private ToastQue toastQue;
  private int count = 0;
  private Random rand = new Random(47);
  public Toaster29(ToastQue tq) { toastQue = tq; }
  public void run() {
    try {
      while(!Thread.interrupted()) {
        TimeUnit.MILLISECONDS.sleep(
          100 + rand.nextInt(500));
        // Make toast
        Toast29 t = new Toast29(count++);
        print(t);
        // Insert into queue
        toastQue.put(t);
      }
    } catch(InterruptedException e) {
      print("Toaster interrupted");
    }
    print("Toaster off");
  }
}

//DiQue用于分配资源;
class DiQue implements Runnable{
  private ToastQue dryQue,peanutQue,jellyQue;
  DiQue(ToastQue dry,ToastQue peanut,ToastQue jelly){
    dryQue=dry;peanutQue=peanut;jellyQue=jelly;
  }
  public void run(){
    try {
      while(!Thread.interrupted()){
        Toast29 t=dryQue.take();
        if(t.getId()%2==0) peanutQue.put(t);
        else jellyQue.put(t);
      }
    } catch (InterruptedException e) {
      print("Divider queue interrupt");
    }
  }
}

class Peanuter implements Runnable {
  private ToastQue proPeanutQue,peanutedQue;
  public Peanuter(ToastQue proPeanut,ToastQue peanuted) {
    proPeanutQue=proPeanut;
    peanutedQue=peanuted;
  }
  public void run() {
    try {
      while(!Thread.interrupted()) {
        // Blocks until next piece of toast is available:
        Toast29 t = proPeanutQue.take();
        t.peanut();
        print(t);
        peanutedQue.put(t);
      }
    } catch(InterruptedException e) {
      print("Peanuter interrupted");
    }
    print("Peanuter off");
  }
}
class Jellyer implements Runnable {
  private ToastQue proJellyQue,jellyedQue;
  public Jellyer(ToastQue proJelly, ToastQue jellyed) {
    proJellyQue = proJelly;
    jellyedQue=jellyed;
  }
  public void run() {
    try {
      while(!Thread.interrupted()) {
        // Blocks until next piece of toast is available:
        Toast29 t = proJellyQue.take();
        t.jelly();
        print(t);
        jellyedQue.put(t);
      }
    } catch(InterruptedException e) {
      print("Jellyer interrupted");
    }
    print("Jellyer off");
  }
}

//define Sandwich;
class Sandwich{
  private static int count;
  private final int id=count++;
  private Toast29[] toast=new Toast29[2];
  Sandwich(Toast29 jellyed,Toast29 peanuted){
    toast[0]=jellyed;toast[1]=peanuted;
  }
  public int getId(){
    return id;
  }
 
  public String toString(){
    return "Sandwich: "+id+", "+toast[0]+", "+toast[1];
  }
  public Toast29.Status getStatus0() {
    return toast[0].getStatus();
  }
  public Toast29.Status getStatus1() {
    return toast[1].getStatus();
  }
}

class Sandwicher implements Runnable{
  private ToastQue jellyedQue, peanutedQue;
  private SandwichQue sandwichQue;
  Sandwicher(ToastQue jellyed,ToastQue peanuted,SandwichQue sandwich){
    jellyedQue=jellyed;
    peanutedQue=peanuted;
    sandwichQue=sandwich;
  }
  public void run(){
    try {
      while(!Thread.interrupted()){
        Toast29 jellyed=jellyedQue.take();
        Toast29 peanuted=peanutedQue.take();
        Sandwich s=new Sandwich(jellyed,peanuted);
        print(s);
        sandwichQue.put(s);
      }
    } catch (InterruptedException e) {
      print("Sandwicher interrupted");
    }
  }
}
// Consume the sandwich:
class SandEater implements Runnable {
  private SandwichQue sandwichQue;
  private int counter = 0;
  public SandEater(SandwichQue sand) {
     sandwichQue= sand;
  }
  public void run() {
    try {
      while(!Thread.interrupted()) {
        // Blocks until next piece of toast is available:
        Sandwich s = sandwichQue.take();
        if(s.getId() != counter++ ||
          s.getStatus0() == Toast29.Status.PEANUT||
          s.getStatus1() == Toast29.Status.JELLY) {
          print(">>>> Error: " + s);
          System.exit(1);
        } else
          print("Chomp! " + s);
      }
    } catch(InterruptedException e) {
      print("Eater interrupted");
    }
    print("Eater off");
  }
}

public class ToastOMatic29 {
  public static void main(String[] args) throws Exception {
    ToastQue  dryQue = new ToastQue(),
              proPeanutQue = new ToastQue(),
              proJellyQue=new ToastQue(),
              peanutedQue = new ToastQue(),
              jellyedQue=new ToastQue();
    SandwichQue sandwichQue = new SandwichQue();
    ExecutorService exec = Executors.newCachedThreadPool();
    exec.execute(new Toaster29(dryQue));
    exec.execute(new DiQue(dryQue, proPeanutQue, proJellyQue));
    exec.execute(new Peanuter(proPeanutQue,peanutedQue));
    exec.execute(new Jellyer(proJellyQue,jellyedQue));
    exec.execute(new Sandwicher(jellyedQue,peanutedQue,sandwichQue));
    exec.execute(new SandEater(sandwichQue));
    TimeUnit.SECONDS.sleep(5);
    print("shutdown Now!");
    exec.shutdownNow();
  }
}
//output:~
/*Toast 0: DRY
Toast 0: PEANUT
Toast 1: DRY
Toast 1: JELLY
Sandwich: 0, Toast 1: JELLY, Toast 0: PEANUT
Chomp! Sandwich: 0, Toast 1: JELLY, Toast 0: PEANUT
Toast 2: DRY
Toast 2: PEANUT
Toast 3: DRY
Toast 3: JELLY
Sandwich: 1, Toast 3: JELLY, Toast 2: PEANUT
Chomp! Sandwich: 1, Toast 3: JELLY, Toast 2: PEANUT
Toast 4: DRY
Toast 4: PEANUT
Toast 5: DRY
Toast 5: JELLY
Sandwich: 2, Toast 5: JELLY, Toast 4: PEANUT
Chomp! Sandwich: 2, Toast 5: JELLY, Toast 4: PEANUT
Toast 6: DRY
Toast 6: PEANUT
Toast 7: DRY
Toast 7: JELLY
Sandwich: 3, Toast 7: JELLY, Toast 6: PEANUT
Chomp! Sandwich: 3, Toast 7: JELLY, Toast 6: PEANUT
Toast 8: DRY
Toast 8: PEANUT
Toast 9: DRY
Toast 9: JELLY
Sandwich: 4, Toast 9: JELLY, Toast 8: PEANUT
Chomp! Sandwich: 4, Toast 9: JELLY, Toast 8: PEANUT
Toast 10: DRY
Toast 10: PEANUT
Toast 11: DRY
Toast 11: JELLY
Sandwich: 5, Toast 11: JELLY, Toast 10: PEANUT
Chomp! Sandwich: 5, Toast 11: JELLY, Toast 10: PEANUT
Toast 12: DRY
Toast 12: PEANUT
Toast 13: DRY
Toast 13: JELLY
Sandwich: 6, Toast 13: JELLY, Toast 12: PEANUT
Chomp! Sandwich: 6, Toast 13: JELLY, Toast 12: PEANUT
Toast 14: DRY
Toast 14: PEANUT
shutdown Now!
Eater interrupted
Eater off
Jellyer interrupted
Jellyer off
Peanuter interrupted
Peanuter off
Divider queue interrupt
Sandwicher interrupted
Toaster interrupted
Toaster off
*/ 1 楼 yeshaoting 2011-03-03   支持一下,我也在断断续续的看Java编程思想.

热点排行