Java中的多线程

Java中的多线程,java,multithreading,Java,Multithreading,我目前正在研究修改版的吸烟者问题。下面你可以找到我的代理类。我需要做什么才能有三个线程而不是一个?因此,将有三个输出,而不是一个 public class agent extends Thread { private table smokingtable; public agent(table pSmokingtable) { smokingtable = pSmokingtable; } @Override public v

我目前正在研究修改版的吸烟者问题。下面你可以找到我的代理类。我需要做什么才能有三个线程而不是一个?因此,将有三个输出,而不是一个

public class agent extends Thread {

    private table smokingtable;

    public agent(table pSmokingtable)
    {
        smokingtable = pSmokingtable;
    }

    @Override
    public void run()
    {
        while(true)
        {
            try {
                Thread.sleep(5000);
            } catch (Exception e) {}
            smokingtable.setAgentElements();
            // this triggers the smoker-threads to look at the table
            output("The Agent puts " + smokingtable.getAgentElements() + table.");
            // pause the agent while one smoker thread is running
        }
    }


    public synchronized void wake()
    {
        try
        {
            notify();
        } catch(Exception e){}
    }


    public synchronized void pause()
    {
        try
        {
            this.wait();
        } catch (Exception e) {}
    }

    private void output(String pOutput)
    {
        System.out.println(pOutput);
    }
}
我做过类似的事情,但这肯定是错误的

public class agent extends Thread {

    private table smokingtable;

    public agent(table pSmokingtable)
    {
        smokingtable = pSmokingtable;
    }

    @Override
    public void run()
    {
        while(true)
        {
            try {
                Thread.sleep(5000);
            } catch (Exception e) {}
            smokingtable.setAgent1Elements();

            output("The Agent 1 puts " + smokingtable.getAgent1Elements());

            smokingtable.setAgent2Elements();
            output("The Agent 2 puts " + smokingtable.getAgent2Elements());

            smokingtable.setAgent3Elements();
            output("The Agent 3 puts " + smokingtable.getAgent3Elements());
            pause();
        }
    }


    public synchronized void wake()
    {
        try
        {
            notify();
        } catch(Exception e){}
    }


    public synchronized void pause()
    {
        try
        {
            this.wait();
        } catch (Exception e) {}
    }

    private void output(String pOutput)
    {
        System.out.println(pOutput);
    }
}

也许我完全误解了你的问题,但看起来你需要再次回顾一下在java中使用踏板的基础知识。这是一个很好的开始

在第二个示例中,看起来您正试图从同一个线程运行所有三个代理,我猜这不是您想要做的

在您给出的第一个代码提取中,将代理id添加为字段并添加到代理的构造函数中,然后将该id附加到输出消息中。 现在,您需要做的就是从某处(可能是您的主方法)创建三个代理实例,并从那里调用它们的run方法

public static void main(String[] args) {
for(int i = 0; i < 3; i++)
  new agent(i).start();
}
publicstaticvoidmain(字符串[]args){
对于(int i=0;i<3;i++)
新代理(i).start();
}

看看这个

为了拥有3个线程而不是1个线程,您需要创建3个线程并启动它们

在您的情况下,最简单的方法是:

Thread agent1 = new agent( );
Thread agent2 = new agent( );
Thread agent3 = new agent( );

agent1.start( );
agent2.start( );
agent3.start( );

agent1.join( );
agent2.join( );
agent3.join( );
更好的方法是使用ExecutorService框架,例如ThreadPoolExecutor

ExecutorService pool = Executors.newFixedThreadPool( 3 );

for ( int i = 0; i < 3; ++i )
{
    pool.execute( new agent( ) );
}

// This will wait for your agents to execute
pool.shutdown( );
ExecutorService池=Executors.newFixedThreadPool(3);
对于(int i=0;i<3;++i)
{
执行(新代理());
}
//这将等待您的代理执行
pool.shutdown();

1。不要扩展
线程
,实现
可运行
。2.不要使用wait/notify,但其中一个
java.util.concurrency
synchronization对象非常有用。还有什么我需要知道的吗?你到底想要实现什么?(我不知道吸烟者的问题)吸烟者的问题是一个众所周知的并发问题,最初由s.s.Patil在1971年描述。假设一张桌子周围有三个强迫性吸烟者,每个人都有无限量的供应——三种必要成分中的一种——一个吸烟者有无限量的烟草,另一个吸烟者有无限量的纸张,第三个吸烟者有无限量的火柴。假设还有一种无烟剂,它含有无限量的三种成分。代理选择了两种成分,并把它们放在桌子上。这样一来,其中一个吸烟者就可以获得他们的供应,并抽一段时间的香烟。与此同时,代理人看到桌子空了,再次随机选择两种配料,并将它们放在桌子上。这一进程将永远持续下去。