PDA

View Full Version : probs with semaphores in java, help me or kill me please!


[TvR] -=CyRuS=-
30-11-2000, 23:35
lo all, dunno if anyone can help me but i have a prob compiling a semaphore class in java.
This is the code for the class:

class semaphore
{
protected int value = 0;

protected semaphore() { value = 0; }

protected semaphore(int initial) { value = initial; }

public synchronized void P()
{
value--;
if (value < 0)
try { wait(); } catch( InteruptedException e) {}
}

public synchronized void V()
{
value++; if (value <= 0) notify();
}
}

and these are the error messages i get:

semaphore.java:17: exception java.lang.InteruptedException must be caught, or it must be declared in the throws clause of this method.
try { wait(); }
^
catch( InteruptedException e) {}

semaphore.java:17: Class InteruptedException not found in type declaration.
try { wait(); }

catch( InteruptedException e) {}
^
2 errors

If anyone has any ideas i cant express how much i would appreciate the help. Tryin to do a project for 3rd year of degree and its got to done in a week.
programmin - dont ya just love it! :)

cheers anyway all who have a look

[TvR]-=CyRuS=- (Marcus)

lol, im so desparate for help i even put my real name, thought it might make it a bit more personal.

Fagat^
01-12-2000, 14:09
Heres the working code:

import java.awt.*;
import java.lang.*;

class semaphore
{
protected int value = 0;

protected semaphore() { value = 0; }

protected semaphore(int initial) { value = initial; }

public synchronized void P()
{
value--;
if (value < 0)
try { wait(); }
catch( InterruptedException e) {}
}

public synchronized void V()
{
value++; if (value <= 0) notify();
}
}

---------------------------------------------------
Guess what you forgot the "r" in Interrupted
InteruptedException :)

[TvR] -=CyRuS=-
02-12-2000, 02:05
If that had not baked my melon so much I could almost find that funny.... :)
cheers anyway mate, you are a life saver.

Concurrent programming, what a nightmare!!!!!!
I only took the module in my degree cuz I thought I was clever... what a fek up.......

anyway thanx again mate... :D

Marcus [TvR]-=CyRuS=-

dirtymb
08-12-2000, 00:23
hehe, seein as u 2 are way above me in expertise, could you help me?

i need to input two scores into an array then compute the total of those, then put the total into a label, then put the two separate into a tooltip box.
i know the basics, but cant make it happen, i've done a GUI and stuff.
plz help, i'll mail you the source
its quite urgent too
cheers!

[TvR] -=CyRuS=-
08-12-2000, 22:00
post it here mate and ill have a look for ya,

filthybugger@hotmail.com

should be able to sort it......... :)

Pizarro
09-12-2000, 23:25
I'll have a look if u want..

send the GUI to me...

D.Pizarro@cs.cf.ac.uk

Fagat^
10-12-2000, 03:08
Dave you got an algorithms assignment to do ;)

Pizarro
12-12-2000, 16:25
Well i need a break from the Algorithm.

RYCHAN
12-12-2000, 22:28
surely the death option would've beeen so much easier :)

Pizarro
15-12-2000, 22:42
Originally posted by dirtymb
hehe, seein as u 2 are way above me in expertise, could you help me?

i need to input two scores into an array then compute the total of those, then put the total into a label, then put the two separate into a tooltip box.
i know the basics, but cant make it happen, i've done a GUI and stuff.
plz help, i'll mail you the source
its quite urgent too
cheers!


The start of the program you can get at

http://www.cs.cf.ac.uk/user/D.Pizarro/Java/Bowling/

sorry it took so long... had loads of C**P to do..

dirtymb
16-12-2000, 23:16
heya cheers for that.
i sent you an email with my crappy version and a question.
how do I get the total score for each player.
I know it adds it up on the fly but I cant find the variable
thx
DMB