Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java并发锁正确终止线程_Java_Multithreading_Concurrency_Locking_Monitor - Fatal编程技术网

Java并发锁正确终止线程

Java并发锁正确终止线程,java,multithreading,concurrency,locking,monitor,Java,Multithreading,Concurrency,Locking,Monitor,我有一个小应用程序,它“模拟”共享打印机系统,包含以下类 所有线程共享的监视器具有以下方法。 reentrantlock锁和下一个方法 public void lock() { lock.lock(); } public void unlock() { lock.unlock(); } public synchronized void refillPaper() { while(FullTray()) { t

我有一个小应用程序,它“模拟”共享打印机系统,包含以下类

所有线程共享的监视器具有以下方法。 reentrantlock锁和下一个方法

public void lock()
{
    lock.lock();
}



public  void unlock()
{  
    lock.unlock();       
}


    public synchronized void refillPaper() {

    while(FullTray())
    {
        try
        {
            this.unlock();
            wait(7000);
            this.lock();
        }
        catch(InterruptedException ex){}
    }

    setPaperLevel(getPaperLevel() - 50);

    System.out.println("Printer refiled");


}


public synchronized void print()
{
    while(!CanPrint())
    {   
        try
        {     
            this.unlock();
            wait();
            this.lock();
        }
        catch(InterruptedException ex)
        {}    
    }
     System.out.println(getPaperLevel()); 
}
然后是线程:用户和技术

用户线程运行方法:

public void run()
{

    Printer.lock(); // the lock() method using the reentrant lock
    System.out.println(this.getNames() + "locked");
    for( int i = 0; i< 4; i++)
    {     
         Printer.print();
         try
         {
             sleep(1000);
         }
         catch(InterruptedException ex) 
         {}
    }
    Printer.unlock();
    System.out.println(this.getNames + " released");
}
和用户技术线程

public void run()
{
    laserPrinter.lock();
    System.out.println("Paper tech acquired the printer: ");

    for(int i = 0;  i < 3; i++)
    {
        Printer.AddPaper();
        try
        {
            sleep(1000);
        }
        catch(InterruptedException ex)
        {}
    }
    Printer.unlock();
    System.out.println("tech released");
}

ThreadGroup users = new ThreadGroup("Users");
ThreadGroup techs = new ThreadGroup("Techs");

Printer printer = new Printer();
User user1 = new User(users,printer);
User user2 = new User(users,printer);
User user3 = new User(users,printer);
User user4 = new User(users,printer);
Tech tech = new Tech(techs, printer);
当线程锁定打印机时,我希望它锁定打印机,直到for循环结束。 目前我遇到以下情况:

用户呼叫打印机。打印4次如果打印机的纸张用完,则技术线程应锁定显示器并重新填充纸张,但其他用户线程访问iTunesr1->user2lock no paper?释放->用户3锁定无纸?释放,直到线程获取它并重新填充纸张,然后什么也不发生

或者,如果有足够的纸张供用户打印,如果技术线程是最后一个,并且条件为false,那么它将等待无限而不是停止

我想我把打印机的锁搞乱了

@帕特里夏·沙纳南,表达得很糟糕。 Printer类是监视器,提供Print和AddPaper方法。 在用户线程的run方法中,我使用for循环调用Print 4 times。在Tech线程的run方法中,我多次调用AddPaper方法

每次调用打印方法时,都会修改纸张级别。 如果纸张过低,则应解锁显示器。
一旦解锁,监视器通常会被另一个用户类型的线程获取。这将导致获取监视器的线程将其释放。这将发生,直到监视器被Tech类型的线程获取。但是在Tech线程被消耗之后,纸张将增加纸张级别,不会发生任何事情。在运行打印方法之前,是否应该返回并运行获取并释放监视器的其他线程。

我在解析用户调用开头的段落时遇到问题,或者如果有。请澄清这些问题。我建议您重新考虑您的整个设置:您的用户根本不需要知道其他用户,打印机应该为任何用户提供打印方法,或者实现自己的打印队列?打印机在纸张用完时应通知tech thread…@Germann Arlington打印和重新填充方法由打印机类提供。打印机类应仅公开打印文档和重新填充纸张方法。您不需要公开lock和unlock方法,因为它们应该由打印机本身(而不是usersOk)在内部使用,但是如果从用户中删除它们,我如何为整个线程运行方法锁定监视器。为整个for循环锁定监视器