简体   繁体   English

运行多个线程的问题

[英]Problems with running multiple threads

Im making an multithread app where the user adds in 1 ingredient at a time to make a fruit salad. 我正在制作一个多线程应用程序,用户一次添加1种成分来制作水果沙拉。 There is a max number of fruits allowed to be put into the bowl. 允许将最多数量的水果放入碗中。

The code compiles and run, but the problem is that it is only running one thread (Apple). 代码编译并运行,但问题是它只运行一个线程(Apple)。 Strawberry has the same thread.sleep(1000) time as apple. 草莓与苹果有相同的thread.sleep(1000)时间。 I have tried changing strawberry's sleep to a different sleep time but it did not fix the problem. 我尝试将草莓的睡眠改为不同的睡眠时间,但它没有解决问题。

Apple.java Apple.java

public class Apple implements Runnable
{
    private Ingredients ingredient;

    public Apple(Ingredients ingredient)
    {   
        this.ingredient = ingredient;
    }

    public void run() 
    {
        while(true)
        {
            try 
            { 
                Thread.sleep(1000);
                ingredient.setApple(6);
            } 
            catch (InterruptedException e) 
            { 
                e.printStackTrace();
            }
         }
     }
}

Ingredients.java Ingredients.java

public interface Ingredients 
{
    public void setApple(int max) throws InterruptedException;
    public void setStrawberry(int max) throws InterruptedException;
}

FruitSalad.java FruitSalad.java

public class FruitSalad implements Ingredients
{
    private int apple = 0;
    private int strawberry = 0;

    public synchronized void setApple(int max) throws InterruptedException 
    {
        if(apple == max)
            System.out.println("Max number of apples.");
        else
        {
            apple++;
            System.out.println("There is a total of " + apple + " in the bowl.");
        }
    }
    //strawberry
}

Main.java Main.java

public class Main 
{
       public static void main( String[] args )
       {
           Ingredients ingredient = new FruitSalad();

           new Apple(ingredient).run();
           new Strawberry(ingredient).run();   
       }
}

Output: 输出:

  • There is a total of 1 apple in the bowl. 碗里总共有1个苹果。
  • .... ....
  • There is a total of 6 apple in the bowl. 碗里总共有6个苹果。
  • Max number of apples. 最大苹果数量。

When you call the .run() method on a Runnable directly within another thread, you simply add that "thread" to the same stack (ie it runs as a single thread). 当您在另一个线程中直接调用Runnable上的.run()方法时,您只需将该“线程”添加到同一个堆栈中(即它作为单个线程运行)。

You should instead wrap the Runnable in a new thread and use .start() to execute the thread. 您应该将Runnable包装在一个新线程中,并使用.start()来执行该线程。

Apple apple = new Apple(ingredient);
Thread t = new Thread(apple);
t.start();


Strawberry strawberry = new Strawberry(ingredient);   
Thread t2 = new Thread(strawberry);
t2.start();

You're still calling the run() method directly. 你仍然直接调用run()方法。 Instead, you have to call the start() method, which calls run() indirectly in a new thread. 相反,您必须调用start()方法,该方法在新线程中间接调用run() See edit. 见编辑。

Try doing that instead: 尝试这样做:

Thread t1 = new Thread(new Apple(ingredient));
t1.start;

Thread t2 = new Thread(new Strawberry(ingredient));
t2.start();

Have your classes extend Thread : 让你的类扩展Thread

public class Apple extends Thread
public class Strawberry extends Thread

Then you can start those threads: 然后你可以启动这些线程:

Apple apple = new Apple(ingredient);
Strawberry strawberry = new Strawberry(ingredient);   

apple.start();
strawberry.start();

You should call join on both threads to wait for them before terminating: 您应该在两个线程上调用join以在终止之前等待它们:

apple.join();
strawberry.join();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM