Re: Help for synchronize threads
- From: "opalpa@xxxxxxxxx opalinski from opalpaweb" <opalpa@xxxxxxxxx>
- Date: 21 Sep 2006 11:10:10 -0700
You're trying to control access to soldiinbanaca, correct?
If so you don't need wait and notify and can synchronize on
Bancomat.class instead of synchronizing on instances.
Opalinski
opalpa@xxxxxxxxx
http://www.geocities.com/opalpaweb/
aehdom@xxxxxx wrote:
I'm very very confused..
How i can apply the methods 'wait()' and 'notify()' for synchronize
threads for this class??
please...
grazie molte..
Fabrizio.
public class Bancomat extends Thread{
static int soldiinbanca;
boolean operazione;
int quantità;
public Bancomat(boolean tipo, int somma){
operazione = tipo;
quantità = somma;
}
synchronized void eseguiOperazione(){
if (operazione == true){
/* Rallentamento che produce (testato)
l'inconveniente di sincronizzazione */
//Thread.sleep(1000);
soldiinbanca = soldiinbanca + quantità;
System.out.println("Versamento di: " +
quantità);
}
else{
if (soldiinbanca - quantità > 0){
soldiinbanca = soldiinbanca -
quantità;
System.out.println("Prelievo di: " +
quantità);
}
else{
System.out.println("operazione non
eseguita!");
}
}
}
public void run() {
eseguiOperazione();
System.out.println("Stato del conto: " + soldiinbanca);
}
public static void main(String args[]) {
soldiinbanca = 1000;
System.out.println("Conto iniziale: " + soldiinbanca);
Bancomat operazione1 = new Bancomat(true, 200);
Bancomat operazione2 = new Bancomat(false, 500);
Bancomat operazione3 = new Bancomat(false, 600);
operazione1.start();
operazione2.start();
operazione3.start();
}
}
.
- Follow-Ups:
- Re: Help for synchronize threads
- From: Lew
- Re: Help for synchronize threads
- References:
- Help for synchronize threads
- From: aehdom
- Help for synchronize threads
- Prev by Date: Re: Array of String/StringBuffers Initalisation
- Next by Date: Re: package private problem
- Previous by thread: Help for synchronize threads
- Next by thread: Re: Help for synchronize threads
- Index(es):