简体   繁体   English

Java线程和同步

[英]Java Threads and Synchronize

I'm working on a lab assignment and I'm a bit stuck on how to implement a specific class. 我正在做一个实验室作业,而我对如何实现特定的类有些困惑。 My task is to create a program that simulates "Trolls" crossing a bridge. 我的任务是创建一个模拟跨桥“巨魔”的程序。 The Trolls are represented by threads. 巨魔由线程表示。 This is part of the Troll class I've written: 这是我编写的Troll类的一部分:

public class Troll implements Runnable {
// instance variables

// constructor

    public void run() {
        System.out.println( name + " has arrived at the bridge.");
        System.out.println( name + " is starting to cross.");

        // simulate crossing time
        for( int i = 1; i <= crossingTime; ++i ) {
            try{
                Thread.sleep( 1000 ); 
            }
            catch( InterruptedException e ) {}
            System.out.println( "\t" + name + " " + i + " seconds." );
        }
        System.out.println( name + " leaves at " + destination + "." );
    }
}

Now the part I'm stuck on is my "Bridge" class. 现在,我坚持的部分是我的“桥梁”课程。 The Bridge class is suppose to ensure only 1 Troll crosses the bridge at a time. 桥类假定确保一次只有1个巨魔过桥。 The bridge class must only have these 2 methods with the same method signature: 桥接类必须仅具有以下两个具有相同方法签名的方法:

//request permission to enter bridge
public void enterBridge() {}
//notify bridge that Troll is leaving
public void leaveBridge() {}

The problem I'm having is figuring out how to make use of these methods. 我遇到的问题是弄清楚如何使用这些方法。 The hint I got from the instructions is to use synchronize. 我从说明中得到的提示是使用同步。 I believe this means to use a synchronized block in enterBridge , but I don't see how this would work. 我相信这意味着要在enterBridge使用同步块,但是我看不到它如何工作。 The code that simulates the actual crossing is in the Troll's run method (this is required by the lab). 模拟实际穿越的代码在Troll的run方法中(实验室需要)。 So to begin crossing, you have to exit the synchronized block. 因此,要开始穿越,您必须退出同步块。 This would release the "lock" and then other trolls will begin crossing before the previous Troll has finished, which is not wanted. 这将释放“锁定”,然后其他巨魔将在之前的巨魔完成之前开始穿越,这是不需要的。

I don't really see the need of the Bridge methods in the first place, since I can just put the code in my Troll's run method in a synchronized block: 我真的没有一开始就需要Bridge方法,因为我可以将代码放在Troll的run方法中的同步块中:

public void run() {
    System.out.println( name + " has arrived at the bridge.");
    synchronized( bridge ) {
        //Code from before
   }
}

This ensures only one Trolls is crossing at a time without needing to call the Bridge class methods. 这样可确保一次只跨过一个巨魔,而无需调用Bridge类方法。 So can someone tell me what the lab is getting at with the Bridge class? 那么有人可以告诉我该实验室在Bridge课程中的表现吗? I feel like I'm overlooking something obvious or maybe I'm misunderstanding how synchronize works. 我觉得自己忽略了一些显而易见的事情,或者我误解了同步的工作原理。

You can easily solve your problem using a semaphore. 您可以使用信号量轻松解决问题。 Semaphores can be found in java.util.concurrent. 信号量可以在java.util.concurrent中找到。 Semaphore are a slightly overkill solution in the case where a single Troll is crossing the bridge at a time, but will become handy when several Trolls will be able to run through the bridge at the same time. 在单个Troll一次越过桥的情况下,信号量是一个过大的解决方案,但是当多个Troll能够同时穿过桥时,信号灯将变得很方便。

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Semaphore.html http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Semaphore.html

阅读关于保护块的文档

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

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