AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

thread e synchronized

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java (Italian)
View previous topic :: View next topic  
Author Message
Cristian
Guest





PostPosted: Mon May 30, 2005 7:36 am    Post subject: thread e synchronized Reply with quote



Scusate, domanda forse banale,
ma come faccio a sapere durante l'esecuzuione di
un programma che lancia 3 thread che accedono ad un metodo
synchronized quali sono quelli in attesa?

quello che volgio ottenere e' un output del tipo

[thread-1] sono in esecuzione....
[thread-1] sono in esecuzione....
[thread-2] sono in attesa....
[thread-3] sono in attesa....
[thread-1] ho finito....
[thread-2] sono in esecuzione....
[thread-3] sono in attesa....

etc etc.

Grazie mille
Back to top
Marco Trevisan
Guest





PostPosted: Mon May 30, 2005 8:20 am    Post subject: Re: thread e synchronized Reply with quote



Cristian wrote:
Quote:
Scusate, domanda forse banale,
ma come faccio a sapere durante l'esecuzuione di
un programma che lancia 3 thread che accedono ad un metodo
synchronized quali sono quelli in attesa?

quello che volgio ottenere e' un output del tipo

[thread-1] sono in esecuzione....
[thread-1] sono in esecuzione....
[thread-2] sono in attesa....
[thread-3] sono in attesa....
[thread-1] ho finito....
[thread-2] sono in esecuzione....
[thread-3] sono in attesa....

etc etc.

Grazie mille
Dalla console di Tomcat, premendo Ctrl+Interr ottengo:

_______________________________________________________________________
Full thread dump Java HotSpot(TM) Client VM (1.5.0-b64 mixed mode):

"TP-Monitor" daemon prio=5 tid=0x0b0174a0 nid=0x624 in Object.wait()
[0x0bb3f000
...0x0bb3f9e4]
at java.lang.Object.wait(Native Method)
- waiting on <0x03564278> (a
org.apache.tomcat.util.threads.ThreadPool$M
onitorRunnable)
at
org.apache.tomcat.util.threads.ThreadPool$MonitorRunnable.run(ThreadP
ool.java:559)
- locked <0x03564278> (a
org.apache.tomcat.util.threads.ThreadPool$Monit
orRunnable)
at java.lang.Thread.run(Unknown Source)

"TP-Processor4" daemon prio=5 tid=0x0af89638 nid=0x614 runnable
[0x0baff000..0x0
baffa64]
_______________________________________________________________________

Fai caso a "waiting on <0x03564278>"

--
Marco

Back to top
stefanomnn@yahoo.it
Guest





PostPosted: Mon May 30, 2005 10:35 am    Post subject: Re: thread e synchronized Reply with quote



per sapere il thead corrente puoi usare Thread.CurrentThread()
se lo usi nel synchronized sai quale thread è nel syncronized: gli
altri stanno aspettando.

Back to top
Cristian
Guest





PostPosted: Mon May 30, 2005 11:03 am    Post subject: Re: thread e synchronized Reply with quote

On Mon, 30 May 2005 03:35:39 -0700, stefanomnn wrote:

Quote:
per sapere il thead corrente puoi usare Thread.CurrentThread()
se lo usi nel synchronized sai quale thread è nel syncronized: gli
altri stanno aspettando.

Faccio cosi:

public void run() {
System.out.println(Thread.currentThread().getName() + " Sono entrato");
querySimulation();
System.out.println(Thread.currentThread().getName() + " Ho finito");

}

public static synchronized void querySimulation() {
System.out.println("Sto eseguendo " + Thread.currentThread().getName());

// eseguo il codice

}

ottengo:
cliente Pippo Sono entrato
Sto eseguendo cliente Pippo
cliente Pluto Sono entrato
cliente Pippo Ho finito
Sto eseguendo cliente Pluto
cliente Pluto Ho finito

Logicamente il mio scopo non e' quello di scrivere l'output ma eseguire
determinati metodia seconda dello status e della posizioni di alcuni
thread.

Non trovo soluzioni migliori :(



Back to top
stefanomnn@yahoo.it
Guest





PostPosted: Mon May 30, 2005 12:21 pm    Post subject: Re: thread e synchronized Reply with quote

ti memorizzi i tre thread da qualche parte (es in variabili di classe
t1, t2 t3)

quindi invece di fare la stampa fai qualcosa del tipo

if (Thread.currentThread()==t1) // fai qualcosa
else if(Thread.currentThread()==t2) // fai altro

se non sbaglio ci dovrebbe anche essere un metodo isActive() nella
classe thread,
quindi puoi anche fare un test del tipo: t1.isActive()...

Spero di averti aiutato!

Back to top
Cristian
Guest





PostPosted: Mon May 30, 2005 3:10 pm    Post subject: Re: thread e synchronized Reply with quote

On Mon, 30 May 2005 05:21:16 -0700, stefanomnn wrote:

Quote:
if (Thread.currentThread()==t1) // fai qualcosa
else if(Thread.currentThread()==t2) // fai altro

se non sbaglio ci dovrebbe anche essere un metodo isActive() nella
classe thread,
quindi puoi anche fare un test del tipo: t1.isActive()...

Spero di averti aiutato!

Si grazie,
per funzionare funziona e, anche se non mi sembra molto "elegante", il
risultato e' quello che volevo :)

ciao,
cristain.

Back to top
Marco Trevisan
Guest





PostPosted: Mon May 30, 2005 3:46 pm    Post subject: Re: thread e synchronized Reply with quote

Cristian wrote:
Quote:
On Mon, 30 May 2005 05:21:16 -0700, stefanomnn wrote:


if (Thread.currentThread()==t1) // fai qualcosa
else if(Thread.currentThread()==t2) // fai altro

se non sbaglio ci dovrebbe anche essere un metodo isActive() nella
classe thread,
quindi puoi anche fare un test del tipo: t1.isActive()...

Spero di averti aiutato!


Si grazie,
per funzionare funziona e, anche se non mi sembra molto "elegante", il
risultato e' quello che volevo :)


Se puoi/vuoi provare darei un'occhiata a java.lang.management del jdk
1.5, personalmente non ho ancora avuto l'occasione di utilizzarlo.
Sarei interessato ad eventuali tue esperienze.
Non so se soddisfa il tuo senso estetico pero' :)

--
Marco

Back to top
Cristian
Guest





PostPosted: Mon May 30, 2005 3:57 pm    Post subject: Re: thread e synchronized Reply with quote

On Mon, 30 May 2005 17:46:31 +0200, Marco Trevisan wrote:

Quote:
Se puoi/vuoi provare darei un'occhiata a java.lang.management del jdk
1.5, personalmente non ho ancora avuto l'occasione di utilizzarlo.


Non ho ancora avuto modo di "mettermi" sull'1.5 per questioni di tempo
ma sicuramente questo sara' un buon motivo per iniziare.

Quote:
Sarei interessato ad eventuali tue esperienze.

Ti faro' sapere

Quote:
Non so se soddisfa il tuo senso estetico pero' Smile

:)


Ciao,
cristian.



Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java (Italian) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.