This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes deadlock static int / performence ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "deadlock static int / performence ???" Watch "deadlock static int / performence ???" New topic
Author

deadlock static int / performence ???

Frank Jacobsen
Ranch Hand

Joined: May 17, 2002
Posts: 335
I have methoed that is call from many difrent portlets:



The pcUniqueName is a class member defined as:

static int pcUniqueName;

I need a unique number in the JVM, this is called from many difrent portlets at the same time....

Can this give deadlock in any way ???

If this cant give any deadlock, and if example 50 portlets calls the mothoed at almost the same time, the last portlet must wait for the other 49 portlets !

Cann i optimize this code in any way ???

Frank
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The risk you run right now is the "race condition". Thread1 might increment pcUniqueName and then Thread2 might increment it again before T1 returns. So T1 and T2 might return the same value. The fix is to synchronize the method or the smallest possible block of code around the incrementer. That will force the 50 portlets to come through the method single-file. Since the method is very fast synchronizing the method won't hurt much.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24081
    
  15

What Stan said goes, but I wanted to give you a little background.

First, "Deadlock" always refers to an interaction that happens when two threads try to lock two different objects in the opposite order. In a deadlock situation, thread A has locked object 1 and is waiting to lock object 2; which thread B has locked object 2 and is waiting to lock object 1. The two threads will wait forever, and this is called a "deadlock." This has nothing to do with your current situation, as there's only one object being used, and in fact no locking at all.

Second, note that threads don't block eachother from calling unsynchronized methods. As Stan describes, many threads can be inside the same unsynchronized method on the same object at the same time. If they're all modifying a common resource, as here, then this is a recipe for disaster!

I'm going to move this to our "Threads and Synchronization" forum.


[Jess in Action][AskingGoodQuestions]
Frank Jacobsen
Ranch Hand

Joined: May 17, 2002
Posts: 335
THANKS
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: deadlock static int / performence ???
 
Similar Threads
Livelock same as race condition?
ActionAdapter
isIdentical understanding
error messages
How to implement dead-lock recovery?