我在这里遇到了一个小问题。我正在用Java做一个并发程序。问题是:有4个人(学生)试图访问打印机,打印5个文档。但是当时只有一个人可以打印(有点明显)5个文件。完成后,他们通知其他人他们完成了,其他线程访问资源。我有一个Main类,学生类和Monitor(激光打印机),Document类,它包含有关文档的信息(如页数,名称用户ID等)+少数打印机接口。我已成功运行线程,但它们不同步(互斥)
所以问题是如何实现互斥(当时只有一个人可以打印他的文件数量)
感谢您的关注,时间和提示:)
主要课程
String S1Name = "bob";
String S2Name = "klara";
String S3Name = "John";
String S4Name = "Iga";
String T1Name = "Man";
String T2Name = "Woman";
final int NoOfDocs = 5;
ServicePrinter sp = new LaserPrinter();
ThreadGroup groupA = new ThreadGroup("Group A");
ThreadGroup groupB = new ThreadGroup("Group B");
Student student1 = new Student(sp,NoOfDocs,S1Name, groupA);
Student student2 = new Student(sp,NoOfDocs,S2Name, groupA);
Student student3 = new Student(sp,NoOfDocs,S3Name, groupA);
Student student4 = new Student(sp,NoOfDocs,S4Name, groupA);
TonerTechnician TT = new TonerTechnician(groupB);
PaperTechnician PT = new PaperTechnician(groupB);
/*
* Start Student Threads
*/
student1.start();
student2.start();
student3.start();
student4.start();
/*
* Start Technician threads
*/
TT.start();
PT.start();
学生班
private final ServicePrinter serviceprinter;
private final int NoOfDocs;
private final String Name;
private final ThreadGroup threadgroup;
public Student(ServicePrinter serviceprinter, int NoOfDocs, String Name, ThreadGroup threadgroup)
{
this.serviceprinter = serviceprinter;
this.NoOfDocs = NoOfDocs;
this.Name = Name;
this.threadgroup = threadgroup;
}
@Override
public void run()
{
/*
* each students prints 5 documents (different name and length)
*/
final LaserPrinter lp = new LaserPrinter();
//sleep from 1 to 5 sec random time
final Random random = new Random();
char[] chars = "abcdefghijklmnopqrstuvwxyz".toCharArray();
StringBuilder sb = new StringBuilder();
/*
* Create random document name 10 characters long
*/
for (int i = 0; i < 10; i++)
{
char c = chars[random.nextInt(chars.length)];
sb.append(c);
}
String docName = sb.toString();
/*
* print 5 documents (random sleep time between printing)
*/
for(int i = 0; i < NoOfDocs; i++)
{
try
{
Document coursework = new Document(Name,docName,random.nextInt(90)+10);
lp.printDocument(coursework);
Thread.sleep(random.nextInt(1000)+4000);
}
catch (InterruptedException ex)
{
Logger.getLogger(Student.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println("User: " + Name+ " completed printing");
监控班级
int tonerLevel = 500;
int paperLevel = 250;
private final String PrinterName = "HP";
private final String PrinterID = "LX-440";
private int CurrentPaperLevel;
private int CurrentTonerLevel;
private int NoOfDocsPrinted;
@Override
public synchronized void printDocument(Document document) {
System.out.println(document);
}
答案 0 :(得分:0)
这是一个简单的互斥实现,但您应该使用java.util.concurrent包进行同步
编辑:将互斥锁更改为信号量(更有意义)
一个简单的互斥实现:
public class Mutex {
private int semaphore;
public synchronized void aquire() throws InterruptedException {
if(semaphore < 0) {
wait();
}
semaphore--;
}
public synchronized void release() {
if(semaphore < 0) {
semaphore++;
notify();
}
}
}
答案 1 :(得分:0)
您似乎在run方法中创建了本地打印机对象,而不是使用传递给Student
类的共享对象。尝试使用您传递的共享打印机,看看你得到了什么。我们还需要了解您在printDocument
中如何使用ServicePrinter
。这是因为您在ServicePrinter
课程中使用的是Student
对象,printDocument
中ServicePrinter
的实施可能不正确(即如果您确实实施了该{{1}}在超类中)