如何在一个线程中执行多个同时动作?

时间:2019-05-27 09:40:48

标签: java multithreading

我需要船只同时装卸货物。 有没有办法在Java中做到这一点?

我设法使多艘船同时在港口工作,但他们先卸货,然后再装新的箱子。

那是我的Ship类的变体

public class Ship implements Runnable {
    String name;
    Port port;
    Queue<Goods> storage;
    Pier pier;
    int capacity;
    int numOnBoard;

    public Ship(String name, Port port, int capacity) {
        this.name = name;
        this.port = port;
        storage = new LinkedBlockingDeque<>(capacity);
        this.capacity = capacity;
        int num=(int)(Math.random()*capacity);
        numOnBoard=num;
        for (int i = 0; i < num; i++) {
            storage.add(new Goods());
        }
    }

    public void run() {
        try {
            int unl = 0;
            int l = 0;
            pier = port.getPier();
            System.out.println("Ship " + name + " taken " + pier.name);
            while (unload()) {
                if(unl>=numOnBoard) break;
                unl++;
                System.out.println("Ship " + name + " unloaded cargo.");
                Thread.sleep(new Random(100).nextInt(500));
            }
            System.out.println("Ship " + name + " unloaded " + unl + " crates.");
            Thread.sleep(100);
            while (load()) {
                l++;
                System.out.println("Ship " + name + " loaded cargo.");
                Thread.sleep(new Random(100).nextInt(500));
            }
            System.out.println("Ship " + name + " loaded " + l + " crates.");
            port.releasePier(pier);
            System.out.println("Ship " + name + " released " + pier.name);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private boolean unload() {
        if (storage.size() <= 0) return false;
        return port.addGoods(storage.poll());
    }

    private boolean load() {
        if (storage.size() >= capacity) return false;
        return port.takeGoods(storage,numOnBoard);
    }
}

还有港口

public class Port {
    Queue<Pier> piers;
    Queue<Goods> goods;
    int capacity;

    public Port(int pierCount, int capacity) {
        goods = new LinkedBlockingDeque<>(capacity);
        piers = new LinkedBlockingDeque<>(pierCount);
        this.capacity = capacity;
        for (int i = 0; i < pierCount; i++)
            piers.add(new Pier("Pier " + (i + 1)));
        int num=(int)(Math.random()*capacity);
        for (int i = 0; i < num; i++) {
            goods.add(new Goods());
        }
    }

    public boolean addGoods(Goods item) {
        if (goods.size() >= capacity) return false;
        return goods.add(item);
    }

    public boolean takeGoods(Queue<Goods> storage, int wasOnBoard) {
        if (goods.size() <= wasOnBoard) return false;
        return storage.add(goods.poll());
    }

    public Pier getPier() {
        Pier taken = piers.poll();
        while (taken == null) {
            try {
                System.out.println("There aren't any free piers. Waiting...");
                Thread.sleep(1000);
                taken = piers.poll();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return taken;
    }

    public void releasePier(Pier pier) {
        piers.add(pier);
    }

    public static void main(String[] args) {
        Port port = new Port(4, 50);
        ArrayList<Thread> ships = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            ships.add(new Thread(new Ship("ship " + (i+1), port, 30)));
        }
        for (Thread t : ships)
            t.start();
    }
}

我需要每艘船同时装卸货物

1 个答案:

答案 0 :(得分:0)

您要在单个线程中完成的任务正是多个线程的目的。多线程使您可以编写一种方式,使多个活动可以在同一程序中同时进行:

  

一个多线程程序包含两个或多个可以同时运行的部分,并且每个部分可以同时处理不同的任务,从而特别是在计算机具有多个CPU时充分利用可用资源。

     

根据定义,多任务是指多个进程共享相同的时间   处理资源,例如CPU。多线程扩展了思想   多任务处理到可以细分特定应用程序的应用程序中   将单个应用程序中的操作转换为各个线程。每   的线程可以并行运行。操作系统不划分处理时间   不仅在不同的应用程序之间,而且在其中的每个线程之间   一个应用程序。

详细了解Java多线程here