• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do i use monitors?  RSS feed

 
KELLY
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this program, i need to use monitors for synchronization, can someone help me, so that monitors are used. please. please. Would be appreciated. Thanks

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ass extends JFrame
{
private JButton requestDVD,returnDVD,exit;
private JTextArea outputArea;
private int DVDLib = 4, StudentsWaiting = 0;
private static int maxT;
private static int minT;
private DVDLib1 D;

// set up GUI
public ass()
{ super("Student DVD rent system");
//create buttons
requestDVD = new JButton ("Request");
returnDVD = new JButton ("Return");
exit = new JButton("Exit");
outputArea = new JTextArea(10,10);
outputArea.setFont(new Font("Times New Roman", Font.BOLD, 12));
//create an instance of inner class ButtonHandler to use for button event handling
ButtonHandler handler = new ButtonHandler();
requestDVD.addActionListener(handler);
returnDVD.addActionListener(handler);
exit.addActionListener(handler);
//get content pane and set its layout
Container c = getContentPane();
c.add(requestDVD, BorderLayout.NORTH);
c.add(returnDVD, BorderLayout.SOUTH);
c.add(exit, BorderLayout.WEST);
c.add(outputArea, BorderLayout.CENTER);
c.add(new JScrollPane(outputArea));
setSize(600,200);
setVisible(true);
}
class ButtonHandler implements ActionListener
{public void actionPerformed(ActionEvent e)
{if(e.getSource() == requestDVD)
{
class requestDVD extends Thread
{ private DVDLib1 req;
public requestDVD(DVDLib1 D)
{super("Ask for a DVD");
req = D;
}
public void run()
{
try{
Thread.sleep((int) ((maxT-minT) + Math.random()*1000));
}
catch(InterruptedException e)
{e.printStackTrace();
}
DVDLib = req.getDVDVal();
StudentsWaiting = req.getStuVal();
if(DVDLib == 0)
{StudentsWaiting++;
outputArea.append("All DVD's have been rented. The queue of DVD's is: " +
StudentsWaiting + " student(s)"+"\n \n");
req.setValues(0, StudentsWaiting);
}
else
{DVDLib--;
outputArea.append("A DVD has been rented out, there is currently " + DVDLib
+ " DVD(s) remaining and " + StudentsWaiting + " student(s) waiting"
+ "\n \n");
req.setValues(DVDLib, StudentsWaiting);
}
}
}

D = new DVDLib1();
requestDVD reqDVD = new requestDVD(D);
reqDVD.start();
}
if(e.getSource() == returnDVD)
{
class returnDVD extends Thread
{ private DVDLib1 Ret;
public returnDVD(DVDLib1 D)
{super("Return a dvd");
Ret = D;
}
public void run()
{
DVDLib = Ret.getDVDVal();
StudentsWaiting = Ret.getStuVal();
if(DVDLib==4)
{outputArea.append("DVD's have all been returned, there are " + StudentsWaiting +
" student(s) waiting" + " \n \n");
Ret.setValues(DVDLib, StudentsWaiting);
}
else
{DVDLib++;
outputArea.append("DVD been returned. There are currently " + StudentsWaiting +
" students waiting to rent" + "\n \n");
Ret.setValues(DVDLib, StudentsWaiting);
}
if(StudentsWaiting>=1)
{--StudentsWaiting;
}
}
}
D = new DVDLib1();
returnDVD retDVD = new returnDVD(D);
retDVD.start();
}
if(e.getSource() == exit)
{System.exit(0);
}
}
}
class DVDLib1
{private int studentWaiting = -1;
private int DVDLib2 = -1;
public int getStuVal()
{return StudentsWaiting;
}
public int getDVDVal()
{return DVDLib;
}
public void setValues(int val1, int val2)
{int StudentsWaiting = val1;
int DVDLib = val2;
}
}
public static void main(String [] args)
/* {
try
{ minT = Integer.parseInt(args[0].trim());
maxT = Integer.parseInt(args[1].trim());
if(minT<0 || maxT<0|| (minT>maxT))
throw new Exception();
}
catch (Exception e)
{e.printStackTrace();
System.exit(0);
}
*/
ass DVD = new ass();
DVD.addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);
}
}
);
}
}
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
KEL,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
Joe Ess
Bartender
Posts: 9426
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When posting huge amounts of code, please use the UBB Code tags to preserve your indenting. Makes it easier on those of us who have to dig through it.
As for your question, the Java Tutorial has a trail dedicated to threads, including how to synchronize access to objects.
[ November 18, 2003: Message edited by: Joe Ess ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!