Java,使用线程

标签 java multithreading synchronized

我正在做一个大型家庭作业,它实现了线程和同步方法的使用。我以前从未使用过 Threads,所以它有点令人困惑。由于作业量太大,我决定先尝试一个简单的例子。所以,在这个例子中我有 4 个类:

  • Food,一个仅被存储的对象。
  • worker “收集”食物并将其储存在仓库中。他的工作时间有限,每次“收集”食物时工作时间都会减少。
  • 存储,用作食物容器,容量有限。
  • Trash - 不是一个对象,它只是用来从存储中删除项目

所以根据定义,Worker 必须是一个 Thread。他的 run() 方法包含一个循环,该循环将使工作人员收集食物(创建新的食物实例)并将其存储在 Storage 中的堆栈中。每次成功的聚会都会减少工作时间。此循环将重复自身,直到工作时间等于 0。现在这是我不明白如何让线程等待的地方。比如说,一个工作人员有 15 个小时,存储容量为 10。因此,工作人员应该在存储中添加 10 个新食品,使其容量最大化,然后等待一些(外部)事件来增加容量或从存储中移除食品这样他就可以继续“收集”食物并将其添加到存储中。这是我当前的代码:

import java.util.*;

class testSync {

    public static void main(String[] args) {
        /** Create a storage **/
        Storage storage = new Storage();
        /** Assign a worker to this storage **/
        Worker worker = new Worker(storage);
        /** Create a trash can **/
        Trash trash = new Trash(storage);

        /** Start new thread **/
        new Thread(worker).start();

        /** The thread should work until the maximum capacity of the storage has been reached **/

        /** Throw item so that further items can be added **/
        trash.throwItem();

    }
}

/** WORKER CLASS **/
class Worker implements Runnable {
    int work = 15;
    Storage storage;
    public Worker(Storage s) {
        storage = s;
    }
    /** Run this method until working hours equal to zero **/
    public void run() {
        while ( work > 0 ) {
            System.out.println(work);
            storage.store(new Food());
            work--;
            /** In case the capacity has been maxed out, wait for some event which will remove food items from the storage **/
            if ( !storage.hasSpace()) {
                // WAIT FOR THE STORAGE TO BE EMPTIED AND THEN CONTINUE ADDING
            }
        }
    }
}
/** TRASH CLASS **/
class Trash {

    Storage storage;

    public Trash(Storage s) {
        storage = s;
    }
    /** Remove one item from the storage **/
    public void throwItem() {
        storage.load();
    }
}

/** FOOD CLASS **/
class Food {
    public Food() {}
}

/** STORAGE CLASS **/
class Storage {

    private int cap = 10;
    private Stack<Food> container = new Stack<Food>();

    public Storage() {}
    /** Check to see if there's any free space **/
    public boolean hasSpace() {
        if (container.size() < cap)
            return true;
        else
            return false;
    }
    /** If capacity allows, add one an item to the storage **/
    public void store(Food food) {
        if (hasSpace()) {
            container.push(food);
        }
    }
    /** Remove one item from the fridge **/
    public Food load() {
        return container.pop();
    }
}

最佳答案

在存储上创建一个同步方法,该方法在接受存储时返回 true。像这样的……

public synchronized boolean store ( int num) {    
   if (  items  < capacity ) {
       items ++;
       return true;
    }
    return false;
}

关于Java,使用线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8546345/

相关文章:

c++ - 在不控制线程的情况下同步 boost::thread 中的 STD cout 输出

java - 为什么 EclEmma 不涵盖 syncronized(MyClass.class)?

java - 什么是NullPointerException,我该如何解决?

java - 如何在四个方向上扩展jScrollPane?

java - synchronized (this) 是只锁定同步块(synchronized block)还是锁定所有 "this"代码?

java - 对象锁定私有(private)类(class)成员 - 最佳实践? ( java )

Java:如何对数组元素进行双重检查锁定?

Java:同步(对象)和RejectedExecutionException

java - 如何从后端创建关于 Spring Security 的 session ?

java - 使用servlet获取网站返回的参数