This java program I am working on seems to hang on startup, so I tried using jconsole to debug the problem.
As it turns out it is waiting on a call to ... |
I'm trying to learn the basic jist of a Semaphore in the Dining Philosopher problem. Right now, I have an array of class Chopstick, and each Chopstick has a semaphore with ... |
Can somebody tell me how can I find out "how many threads are in deadlock condition" in Java multi-threading application? What is the way to find out the list of deaklock ... |
I have a relatively simple (perhaps stupid) question regarding synchronisation in Java.
I have synchronisation blocks that acquire locks on various objects throughout my code. In some scenarios, I want to acquire ... |
I need to determine which locks are the most-contended-for in my application code. What free tools can I use to determine this ?
|
Seeing various locking related question and (almost) always finding the 'loop because of spurious wakeups' terms1 I wonder, has anyone experienced such kind of a wakeup (assuming a decent hardware/software environment ... |
Let us say that one Thread is executing inside a Synchronized Function in Java and another Thread wants to access the same method but it will have to wait till the ... |
|
Our application has ~10 threads doing separate tasks (no thread pools). We are not experiencing deadlock, but are always trying to lower latency to respond to a request so we ... |
I read ConcurrentHashMap works better in multi threading than Hashtable due to having locks at bucket level rather than map wide lock. It is at max 32 locks possible per map. ... |
I know in C# when you have an object you want to use as a lock for multi-threading, you should declare it as static inside of a class, which the class ... |
I have a java program, with multi threading. My problem is that from time to time, I have a timeout.
Because I have some unix stuff I cannot see where is the ... |
I'm trying to delete a file that another thread within my program has previously worked with.
I'm unable to delete the file but I'm not sure how to figure out which thread ... |
I've got a question related but not identical to my first question ever here:
http://stackoverflow.com/questions/2340114/java-what-happens-when-a-new-thread-is-started-from-a-synchronized-block
Is it a common practice to create and start() a new Thread when you're holding a lock? ... |
My program has a server thread and separate client threads that are individual connections to other servers. So how my program works is the client threads make individual requests, and when ... |
I have a Java application using threads, which uses several Lock object instances to synchronize access to common resources.
Now as part of a performance measurement I want to measure the ... |
What is a class level lock. Can you please explain with an example.
|
Class ThreadTest extends Thread {
public synchronized void run() {
}
public static void main(String args[])
{
Thread t1=new ThreadTest();
Thread ...
|
I have to call a function 3rd party module on a new thread. From what I've seen, the call either completes quickly if everything went well or it just hangs for ... |
When we talk about intrinsic lock we refer to the object for which we ask the lock
or for the synchronized method?
The lock is on the object or on it's sync method?
I'm ... |
Threads A, B, C in that order, reach synchronized method f() in a single object.
All have the same priority.
B and C are blocked.
A leaves f().
What thread now begins running in ... |
I have multiple threads which are serializing my 'Data' objects to files. The filename is based on 2 fields from the Object
class Data {
org.joda.DateTime time;
... |
Right now i am trying to learn more about java threading, and i have a small question that i cannot find a direct answer to anywhere. Lets say i have two ... |
If a thread T1 enters a method m1 by obtaining the class level lock, does this mean another thread T2 cannot run a different method m2 by obtaining the object level ... |
I'm load testing a webservice which writes to a lucene index. If I make the same call repeatedly I get a
org.apache.lucene.store.LockObtainFailedException:
I assume this is because I'm trying to write to ... |
I need to implement the following scenario
- ThreadN acquires a lock
- ThreadM tries to acquire the lock and wait
- ThreadX (monitor) sees that ThreadN is holding lock too long time and releases the lock
- ThreadM ...
|
It boils down to one thread submitting job via some service. Job is executed in some TPExecutor. Afterwards this service checks for results and throw exception in original thread under certain ... |
I am trying to implement nodes talking to each other in Java. I am doing this by creating a new thread for every node that wants to talk to the server.
When ... |
Recently, I came across a volatile flag technique, which I may avoid from using synchronized or lock.
http://jeremymanson.blogspot.com/2008/11/what-volatile-means-in-java.html
Here is one of the code example of using volatile flag.
Correct Working Example
Thread ...
|
I read that the locks in java are obtained on the instance(object) basis (in case of instance method)
Also the locks can be obtained on the object of class (in case of ... |
I have a synchronized method in a singleton class which is called by many threads simultaneously. Is there any java API available to check which thread is currently owning the lock ... |
I have a map which is of type Collections.synchronizedMap . The map contains database tables.
Now I have 3 methods and two lock. The first lock around each database table in a ... |
So i used the following to create a lock on a file so that I can edit it exclusively:
File file = new File(filename);
channel = new RandomAccessFile(file, "rw").getChannel();
lock = ...
|
Greetings, I am curious if there is someway to check if ArrayBlockingQuery query is currently locked ?
The reason for this : I have a server which is listens to a ... |
I have to write this produce consumer application using multithreading. I wrote the following java code but havn;t been able to figure out where it is getting wrong. Also i want ... |
Ex 1)
public void addName(String name) {
synchronized(this) {
lastName = name;
nameCount++;
...
|
Can someone please explain the difference between these two examples in the context of object locking:
public void method1(){
synchronized(this){
....
...
|
I'm developing a Java app that for much of the time, including the point of shutdown, is having to deal with a flood of incoming asynchronous calls from a foreign framework. ... |
Is there a way to do something like this in Java (possibly using Lock instead of synchronized):
synchronized(lock) {
Thread t = spawnThread(new Runnable() {
...
|
The JVM performs a neat trick called lock elision to avoid the cost of locking on objects that are only visible to one thread.
There's a good description of the trick here:
|
In my current project I notice that select() do not block as expected.
It do not block at all and return always, even when no IO was present. So I got a ... |
I just want wait in main thread until some event in run.
How can I do it using java.util.concurency classes?
Thanks!
P.S.
Does my question realy explained bad o correctness check is shit? ... |
Let's say I have this java code:
synchronized(someObject)
{
someObject = new SomeObject();
someObject.doSomething();
}
Is the instance of SomeObject still locked by the time doSomething() is called on ... |
I have Condition variable named cond.
Is there any method which could give me true or false if there is any thread awaiting on cond?
I need something like:
... |
Code like this...
public void beforeUpdated(Log log){
synchronized(this){
query(log);
merge(log);
persist(log);
...
|
i'm using some ReentrantLock to synchronize access to a List across multiple threads. I just write a generic
try {
lock.lock();
... modify list here
} finally {
...
|
I have the following code
public class Test {
Lock lock = new ReentrantLock();
public static void main(String args[]) throws Exception {
Test t = new Test();
Second ...
|
47. Locks coderanch.comI am totally confused about locks(in Thread ). Also I have seen in one of the postings the following. (http://www.javaranch.com/ubb/Forum24/HTML/000643.html) " a lock can be obtained using wait() method. But, wait is a method of Object class and thus every object can use wait() method ". How one will get lock by calling wait() method?? I've read that wait() is to ... |
Hi, This is from RHE; How many locks does an object have? 1. But every object can have class level lock and instance lock.So, doesnt it make 2 locks. Correct me where i am wrong? 2)A monitor is an instance of any class that has synchronized code. True/fals This is true for instance methods.But for class locks the monitor need not ... |
A Mutex is a concept relating to mutual exclusion locks Mutual exclusion locks (mutexes) prevent multiple threads from simultaneously executing critical sections of code which access shared data (that is, mutexes are used to serialize the execution of threads). All mutexes must be global. A successful call to acquire a mutex will cause another thread that is also trying to lock ... |
Hi Java Gurus... I have implemented a lock manager which basically keep tracks of exclusive locks. I've found a bug but don't know how to fix it. Please help me if you know the solution. I am really desperate for some help. Here's a part of my source code. The problem is inlined as comments. public void lock(String resourceName) { ResourceLock ... |
Hello all, when a wait() is invoked, the current thread relinquishes the lock on the object on which wait was invoked. But what exactly happens with following scenario; synchronized(object outerObject) { // do something... synchronized(object innerObject) { // do somethging... while(some condition....) innerObject.wait(); // continue again....... }// Give up innerObject lock }// Give up outer object lock My question: ============= 1) ... |
In fact, you can find out, I just discovered from my learned boss: The wait() and notify() methods on java.lang.Object require that you hold the lock on the object. If you do not hold the lock, these methods will throw IllegalMonitorStateException. This gives a way to determine whether you hold the lock. The usual pattern for using wait() or notify() is ... |
This code tries to show that stop does release the monitor. The thread toStop is started. It prints "ToStop still running" forever after having started the thread test. This second thread prints "test has started and now is waiting to adquire the lock" and it mus wait for the thread toStop is stopped because both threads has been synchronized over the ... |
First, the fact is that it's threads that are actually running and seeking & releasing locks. Every instance in the RAM is represented by at least a thread. Then, let's take a close look at the lock issue: When the instance (or a thread) is running and is up to the time to invoke a synchronized lock, it enters the stage ... |
Mike The lock, in this case, is on the object refered to by the object variable. Once it enters the synchronized block it gets the lock associated with that object and holds it until it leaves the block. As soon as it leaves the block the lock is released. hope that helped |
The JLS states that: The method wait should be called for an object only when the current thread (call it T) has already locked the object's lock. Suppose that thread T has in fact performed N lock actions that have not been matched by unlock actions. The wait method then adds the current thread to the wait set for the object, ... |
Hello friends, in that i had one problem : Let say there are 2 thread ReaderThread and WriterThread.and one vector which had some values. readerth reads from Vector and WriterTh writes into vector . before reading from vec. i put lock onto vect. and then i read the data from it.so taht no other will access this vect.and after read'g is ... |
Would anyone object to a simple, basic thread question? My question is not motivated by a practical problem. I am only reading about threads. In Java Thread Programming, Paul Hyde says When a lock is released, all the threads waiting for it compete for exclusive access. Only one will be successful, and the other threads will go back into a blocked ... |
|
every object has a lock,but consided the "abstract" class,that isn't instance,if it has a synchronized method ,i invoked the method from the driver class,i will receive the which locks? the following class indicate my means: abstract class BaseClass { public synchronized void f(){} public Synchronized void g(){} } public class Myclass extends BaseClass { public static void main(String[] arg) { Myclass ... |
|
I would like to find out if there is an extra sigificant overhead of grabbing the same lock more than once. e.g. void fooA() { synchronized (m_lock) { ... } } void fooB() { synchronized (m_lock) { fooA(); } } When I call fooB(), the flow would grab m_lock twice. I feel this is a design tradeoff. I could have removed ... |
If you mean different processes - different JVMs then I think this is possible by introducing your custom file lock which would act like object lock, i.e. say first process creates some file (eg .lock) another process checks for presence of this file and vice versa. When access is finished lock file is removed. |
Hi, Those locks are not related, and each needs to be obtained regardless of the other. Namely, if you have: class Foo{ static void staticMethod(){} void instanceMethod(){} } And then an instance: Foo f=new Foo() Then you have 2 unrelated monitors (one for class Foo, one for instance f ). Any threads attempting to invoke staticMethod() will need to gain access ... |
Hi All, I have two objects that are monitors. A client calls a method on the first, which is synchronized so therefore gets ownership of the first objects lock. The client then enters a method from the second object, which is also synchronized, and so the thread now owns two locks. The client then calls wait on the second object. The ... |
Hi All, I have a little problem with it comes to thread safe locking that I cannot seem to solve. The problem is that a thread has to own 2 locks on two different objects, it then needs to wait on one of them, but somehow before waiting, release the lock on the first. This is basically a database problem, I ... |
Originally posted by karthik Guru: What are nested locks and have people run into Nested Locks situation in practice? Not sure what you mean by "nested locks"... in our book, we simply define it as the fact that Java synchronization locks can nest. It basically means when a thread that owns the lock, trys to acquire the lock, it is granted ... |
Dear All, I am newbie to this threading concepts. Can someone explain how does the class lock differs in the object Lock. All i knew is the if the static method in a class is synchronised then it acquires the class lock while the instance methods which are synchronised acquires object locks. I assume that the class files which we do ... |
hi iam i correct in stating these 3 statement. 1> when u synchronize a method, lock of object used to invoke this method should be acquired. 2> method() { synchronize(this) { } } above code lock is obtained on object used to invoke the method 3> method() { synchronize(a){} } in the above code lock is obtained on object refered by ... |
Every object has a lock - tho lock is not exactly the right word but the right one isn't coming to me just now. The lock can have no object holding it or one object holding it. When thread T1 holds the lock and thread T2 asks for it, T2 will wait until T1 gives up the lock. When you make ... |
|
Hi, I have a small doubt in Thread. Whether lock is for methods or objects. For example: I am having a object 'a' which has three methods 'method1()' and 'method2()' and method3(). Of which, mehtod1() and method2() are synchronized. Object a: --------- synchronized method1(){} synchronized method2(){} method3(){} Whether a thread by name 't1' now acquiring lock on Object a or method1()? ... |
|
To the best of my knowledge, yes. At least, Jeremy Manson and Brian Goetz say so, and they're in a pretty good position to know. However if you really need a singleton, the Holder class idiom (discussed in the same link) seems to work better anyway. Of course other issues with singletons still exist, like is global data a good thing, ... |
I would have thought that both of your examples would wait in wait(), because you have no notify() calls anywhere. Using a Thread object as a monitor for synchronize is legal, but not advisable. Who knows what other system things might be synchronising on the Thread object?! Use an object over which you have full control. |
76. locking coderanch.com |
Hi, Is the following lock on the inner object legal ? It is meant to achieve the following: The purpose of this inner object lock is to enable the administrator to editUser() or deleteUser() even if someone is doing an addUser(). Does acquiring the lock on the inner object require that we also acquire the lock on the outer object ? ... |
Is this code below Not properly synchronized? Multiple threads could enter this part of a method... I found this. It seems to me that the LOCK is local thus if a thread tried to lock on it, it'd never be locked because each thread would have its own local LOCK! Am I correct? if(cacheSuccessful && tempMap != null && !tempMap.isEmpty()) { ... |
|
Hi! Q 1. I enter a synchronized block, throw an Exception and enter a catch block. At what point did I release the lock ? When I threw an Exception, or when I finished the catch block or did I never? Q 2. Does making the lock object static, have any influence on any threads ability to execute? thanks [ July ... |
wait() --- releases lock sleep() --- keeps lock notify()--- keeps lock notifyall()--- keeps lock join() --- releases lock yield() ---- keeps lock Okay, I feel I have to explain the join() method. The fact that join() releases the lock is merely a side-effect. Normally, when you use the join() method, you don't grab the lock, so you don't notice that join() ... |
Hi - need a bit of advice. I have a program that moves files from one directory to another (and then does some stuff...). The program has multiple threads running e.g. //in main XmlCompare xc = new XmlCompare(); Thread t1 = new Thread(xc); t1.start(); XmlCompare xc2 = new XmlCompare(); Thread t2 = new Thread(xc2); t2.start(); The problem is that while one ... |
Howdy "l4strada", Welcome to the JavaRanch... You may not have noticed that we have a policy on screen names here at the ranch. It must consist of a first name, a space, and a last name. It must also not be fictitious. Unfortunately, your screen name does not seem to conform with this policy. Please take a moment to change it. ... |
Hello, I'm trying to understand the behavior of the Reentrant locks classes when used in conjunction with Hashmap. I'm attempting to use the put() method within a lock.writeLock().lock(). So for example: code starts here ....... lock.writeLock().lock(); .... someHashmap.put(myValue, yourValue); ... .. finally { lock.writeLock().unlock(); } The write lock is actually used to protect concurrent access to a database. Also, any good ... |
class SyncTest implements Runnable { private NewThread nt = new NewThread() ; public void run() { doStuff() ; doSomethingElse() ; } public void doStuff() { System.out.println("not synchronized") ; synchronized(nt) { nt.methodShowing() ; for(int i=0; i<20 ;i++) { System.out.println("This is "+Thread.currentThread().getName()+" executing in synchronized block"); try { Thread.sleep(1000) ; } catch(InterruptedException ioe) { ioe.printStackTrace() ; } } } } public synchronized void ... |
I have a class that has two synchronized methods. One sets the Sring, and then prints it 3 times. The next method changes the String to "concurrency error" but that is the end of the method. Now as they are locking the same object the "concurreny error" and that is the end of the method. Theorectically as they lock the same ... |
Hi all, I have a question regarding the synchronization of method.. can a thread access the non-synchronized method of an instance of class when the other thread have the lock on the same instance of class? because as i know the lock is for object , so if a class contains both synchronized and non synchronized methods , and if one ... |
A couple questions about threads. 1. When this code executes, what happens? String a = new String("temporary string"); synchronized(a) { a = new String("second temporary string"); while(true) { // more code here // } } I think the lock should be lost since a new "a" object is created but the synchronized code is still executing. Is this correct? 2. When ... |
Hi, I am using a ThreadPoolExecutor to send messages to a native application. The method is in the code pasted below. When I run the method with a test harness, the code works fine. But, when I run with my application, which has a lot of logging and other stuff going on, the worker threads initiated by the ThreadPoolExecutor lock up ... |
I am using the Head First, K&B book there is the example of Ryan and Monica's bank account problem, where it gets Overdrawn due to withdrawal method not being synchronized. I have gone thru this many times and also experimented with it in Eclipse. Below is code snippet: public class RyanMonicaJob implements Runnable{ public static void main(String[] args) { RyanMonicaJob job ... |
|
I'm using the java.util.concurrent.locks.ReentrantReadWriteLock and the code to upgrade a lock from read to write has a very ugly finally to unlock the last read. There has to be a better way. The basic code looks like // in the class top /** lock for safe multithreading */ private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); /** read lock alias for above ... |
Originally posted by Yohan Liyanage: In Terracotta, it is possible to have a lock which works across JVMs. How do they achieve that? First, I have never used it, so this is *not* an endorsement (of whether it works or not). I just happened to have done some research of Terracotta a few months ago... Yes, it is possible to have ... |
Unfortunately, double-checked locking is not guaranteed to work because the compiler is free to assign a value to the singleton member variable before the singleton's constructor is called. If that happens, Thread 1 can be preempted after the singleton reference has been assigned, but before the singleton is initialized, so Thread 2 can return a reference to an uninitialized singleton instance. ... |
Please help me to answer below question related to object locks in Multithreading. Consider the example in a class where there are two methods. One synchronized method and other Unsynchronized i.e S1 and S2. I am creating two threads using same object of class i.e T1 and T2. My question is:- When thread T1 is accesing synchronized method S1, it will ... |
Dear guys, Below is the progam I've been writing. It has two threads, one (PanelView) is for displaying the data in the file, and one (DataCreator) is for writing the data into the file. In the middle there is an object to synchronize the two threads. The object has two methods, to read and to write data. To synchronize, right now ... |
I understand that each object has its own lock and (non-static) synchronised code cannot be entered by a thread if another thread already has taken that particular object's lock. I've just read (Head First Java P.522) that static methods can be synchronised and that in this case a class lock is taken. My question is ; if thread A is running ... |
Originally posted by Pho Tek: Does this mean that if a thread is blocked on lock.lock(), the thread is screwed because lock() is not interruptible ? What is the utility of a non-interruptible lock ? The un-interruptable lock will block when lock.lock() gets called until the thread that currently owns the lock releases it. If the thread that currently owns the ... |
I don't know if it's new, but I have a new-to-me locking idea. I was hoping to get your advice on it. So, it's a simple lock which is acquired and released. Acquisition is just through the acquire() method, which returns a lock instance. The unique part is that there is no release method; release is effected by 'losing' the lock ... |
Hi friends, We are using EJB3 in our application and we've created an interceptor to check whether the user has already sent a request or not. if we noticed that the user has already sent a request then in this interceptor we need to suspend that thread and wait till the former request being completed otherwise the thread will proceed. in ... |