I have a static boolean which is read by many threads but only set by one. Is it necessary to wrap accesses to the boolean in a synchronize(myLock) block. It is not imperative that a reading thread sees a change which occurred concurrently, it will pick it up one second later. Thanks [ September 02, 2008: Message edited by: Robert Kennedy ... |
The code below will work; not that I made Helper volatile. Effective Java 2nd edition ( Bloch ) has a nice section on this. class Foo { private volatile static Helper helper = null; public Helper getHelper() { if (helper == null) synchronized(this) { if (helper == null) helper = new Helper(); } return helper; } } |
|
Well, "it doesn't make any sense" is a bit overboard. Maybe "inefficient" is a better way to put it. It is possible to have thread unsafe portions of code, that include waiting periods. I can make a request for a service, wait for the result, make another request, etc., and have some state between requests, that releasing a lock may not ... |
|
Hi All, I am using jdk1.4, I got a problem in following peice of code. My requirement is that 'refreshAll' function should be executed by one thread/request at a time AND meanwhile if any other thread/request attempts to run it then user should be prompted with error message that "operation already being performed by other user". if(operation.equals("refreshAll")){ synchronized(this){ refreshAll(scope); } } ... |
Hello all i have multithreaded problem on which i have method that multiple threads are accessing this method is responsible on setting value this value needs to be set only once ( then it lives in all the application life time ). how can i make sure that only the first thread that comes will set access this method and preventing ... |
|
|
Hi all I am extending a processing engine to be cluster aware. There is a lock manager component that sits only on the master node. This lock manager currently uses the Java 5 reentrant lock, and on a single node this works very nicely. The problem is that when a slave node goes down, I want to be able to release ... |
Hi Team, I have a service/thread which reads the xml data from the given RSS XML feed URL and writes to a file. This service got hangs sometime twice in a week and by looking at the jstack process dump it seems a thread lock appears during the RSS xml Feed URL connection for getting XML feed data. Please see the ... |
|
I am currently interested in the subject of persistent locking, i.e. acquiring locks in a database. Last year I was involved in several projects with such requirement, mainly on account of running in clustered environment. In such case there is sometimes a need to ensure that a resource / critical code block is subject to synchronized modification / execution beyond the ... |
|
when a lock on an object is attained by one thread when it run a synchronized method, the other thread of the same object can run the other non synchronized methods of the class without any restriction. Is this true?Please tell me the reasoning coz i believe that the lock is attained on an object in this case and other thread ... |
When we put synchronized on some object it acquires intrinsic lock associated wit that object . Similarly when we put synchronized on some function it will acquire the intrinsic lock on "this" object . Now my problem is there anyway to know whether the object is already locked or not . For ex , say i have a class class SyncExample{ ... |
I am learning thread, read this article in wiki. http://en.wikipedia.org/wiki/Double_checked_locking_pattern // Broken multithreaded version // "Double-Checked Locking" idiom class Foo { private Helper helper = null; public Helper getHelper() { if (helper == null) { synchronized(this) { if (helper == null) { helper = new Helper(); } } } return helper; } // other functions and members... } it said For ... |
I know synchronization used for thread lock. I need clear explanation with one example. Can you explain about thread concept.How i know one thread is locked, and how can i force or acquire another thread for lock . Finally how to know thread is released from lock. Explain types of lock(like method level and block level) in thread. |
hi guys i need to know the difference between class level lock and Object level lock i know that the Object level lock can be achieved by synchronized blocks if i need no other threads to acess the object. but what is the necessary of Class level lock.and what conditions i have to think about Class lock thanks amir |
class MyRunnable implements Runnable { Integer i = 1; } public void run() { synchronized (i) { for (int i = 0; i < 100; i++) { try { Thread.sleep(0); System.out.print(Thread.currentThread().getName() ); } catch (InterruptedException ex) { Logger.getLogger(MyRunnable.class.getName()).log(Level.SEVERE, null, ex); } } } // System.out.println(Thread.currentThread().getName() + " : i am out of run"); } } public class StaticSync { public static ... |
|
Is there a way for java to lock a specific instance variable? for example, i want to lock the product object assuming it's id is 1 (with fields such as: prod id, product name and product price), so that no other threads can access product object with id 1 while other product object with different id (of course not 1) can ... |
|
Hi, I want to know how the threads will wait for following cases: 1: public class One{ public synchronized void getA(); public synchronized getB(); } 2: public class Two{ public void getA(); public void getB(); } 3: public class One{ public void getA(); public static void getB(); } 4: public class One{ public static void getA(); public static void getB(); } ... |
Hi, I'm relativelt inexperienced in threading so was wondering if someone could help me out. I have just had an interview where they asked my to describe a class that prices a trade. The pricing method should return a new price only if it hasn't been priced before otherwise it returns a previosly calculated price and many threads can be executing ... |
I have one doubt regarding class level lock and object level lock. It is clear for me if we want to synchronize our static methods then we make these method synchronized and take a class level lock for this. But my doubt is: If we have a static method that accesses a non-static field (using an instance) and same non static ... |
|
The following example is a violation. public synchronized void method(Object obj) { synchronize (obj) { obj.wait(); } } The wait call releases one of the locks on the monitor "obj". To release the rest of the locks held, do we call wait in a while loop? Is this the right solution? If we don't release the rest of the locks, will ... |
Am I correct in assuming that a lock behaves similarly to synchronized code in the following way - If I have the following class below: Class X { private ReentrantLock lock = new ReentrantLock(); public increment() { lock.lock(); try { // some stuff } finally { lock.unlock(); } public decrement() { lock.lock(); try { // and some other stuff } finally ... |
Hi, I am trying to create and see a race condition by running a very common double checked locking code. Instead I am seeing a NullPointerException at the bold line underneath, which I don't want to. package com.rbs.threads; //Double Checked Locking public class SomeClass { //One way to resolve the problem is to make resource field static, that will be like ... |
Hi All, I have a class as shown below: public class ThreadSafeImpl { public synchronized void firstMethod(){ System.out.println("Executing first method!"); } public synchronized void secondMethod(){ System.out.println("Executing second method!"); } } this class has two methods they are synchronized. Now let us say thread 1 is executing firstMethod while its executing that thread 2 tries to execute the second method on same ... |
import java.io.File; import java.io.RandomAccessFile; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; import java.nio.channels.OverlappingFileLockException; public class FileTest implements Runnable { public void run(){ System.out.println("Inside Run..."); File file = new File("C://CMPS/Migration/Test/Response File.txt"); System.out.println("file"+file); System.out.println(file.exists()); try{ FileChannel channel = new RandomAccessFile(file, "rw").getChannel(); System.out.println("Channel "+channel.toString()); FileLock lock = null; try { System.out.println("inside second try....."); lock = channel.tryLock(); System.out.println("inside second try----->"+lock); } catch (OverlappingFileLockException e){ e.printStackTrace(); System.out.println(" Exception "+e); ... |
Hi, i am developing file lock concept in java. i am using two threads. first thread is in running state...second thread will wait untill first thread release... i cant make second thread will wait.. i am getting some exception... import java.io.File; import java.io.RandomAccessFile; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; import java.nio.channels.OverlappingFileLockException; public class FileTest implements Runnable { public void run(){ System.out.println("Inside Run..."); File ... |
hello ranchers I have a scenario where when a call to particular method is being invoked it shouldn't be able to be invoked by other instances.Take a look into the following code. public class MyClass{ public synchronized void logic(){ //some functionality Thread.sleep(10000);//want to see if other invocation is possible during this time. } } public class Test{ public static void main(String[] ... |
Q1: When One Thread holding Static lock(Class lock) on a Class then is it possible for another Thread to acquire Instance lock of this class Q2: When One Thread holding the Instance lock then is it possible for another Thread to acquire Static lock(Class lock) on this class public static synchronized void display(){ System.out.println("i am in static dis"); } public synchronized ... |
public class test implements Runnable{ public void run(){System.out.println("run method of "+Thread.currentThread().getName()); synchNonStat1(); synchNonStat2(); nonsynchNonstat1(); nonsynchNonstat2(); } public synchronized void synchNonStat1(){System.out.println("synchNonStat1");} public synchronized void synchNonStat2(){System.out.println("synchNonStat2");} public void nonsynchNonstat1(){System.out.println("nonsynch-nonstat11");} public void nonsynchNonstat2(){System.out.println("nonsynch-nonstat22");} /*public static void nonSynStat1(){System.out.println("synch--stat11");} public static void nonSynStat2(){System.out.println("synch--stat22");} public synchronized static void synStat1(){System.out.println("synch--stat11");} public synchronized static void synStat2(){System.out.println("synch--stat22");}*/ public static void main(String[] args) { test testobj1=new test(); Thread t1=new Thread(testobj1); t1.start(); ... |
If I make a method static then the thread executing that method would acquire class level lock , now if another thread tries to access some other method of the same class, would it have to wait until the first thread releases the lock on class. So my question is : Does a class level lock lock the entire class ? ... |
I think the answer depends on what it actually means to "update" the key of an entry. There are three possible meanings for that: (1) Change the internal state of the object which is the key. (2) Change the internal state of the object which is the value. (3) Assign a new object to be the value. It does help to ... |
I was reading this thread about why double checked locking fails in a multi-processor environment. http://www.javaworld.com/javaworld/jw-02-2001/jw-0209-toolbox.html?page=4 In the page 3, the author mentions that - "To summarize, synchronization implies a memory barrier. Consequently, though the code within the synchronized block is subject to reordering, all modifications made within that block will be visible in main memory (for other threads to view) ... |
Hi guys, I have the following code: package pt.ptin.threads; import java.util.HashSet; public class Main { /** * @param args */ public static void main(String[] args) { Classe1 c1 = new Classe1(); System.out.println("Thread id: " + Thread.currentThread().getId() + " running main(), name " + Thread.currentThread().getName()); t t1 = new t(c1,"b", Thread.currentThread()); System.out.println("First Thread id: " + t1.getId()); t1.start(); try { Thread.sleep(1000); } ... |
Hello all, I wonder if someone could help me understand the behavior of Java FileLock. I have one java program to lock the file: import java.io.*; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; public class FileLockTest { public static void main(String args[]) throws Exception { // Get a file channel for the file File file = new File("/u01/app/fmw/outbound/pmdm/FMW_PM_KLMUT_20110831.DAT"); FileChannel channel = new RandomAccessFile(file, "rw").getChannel(); ... |
Hi, Everyone I was asked this question in an interview, to implement a Cache by using HashMap, but we want to allow different key's value to be updated by different thread at the same time. For example, thread 1 update key1 with value1, thread 2 is allowed to add new key2 with value2, thread3 has to wait to update key1 until ... |
|
Hi there. I have a question about when a thread acquires an objects lock. Just to get some facts straight in my head: When a thread gets a hold of an objects lock, does it hold onto that lock even when the thread is moved from a running state back to a runnable state, say, due to time-slicing? Thanks and regards. ... |
ok so lock is released if we are putting code in try-catch block. . right ? 1) But suppose what abt runtime exceptions which i am not catching. 2)Also the thread will be suspended in both the cases or not ( both cases mean for runtime exception and checked exception) Thanks .. |
I have a situation where i have more then 20 threads each has some variable call it- int [ ] variable. Now each thread wants to see this variable of all other thread and any thread is allowed to add value of int array of any other thread. Now suppose a reference to int array or all threads is availabe to ... |
I was always under the impression that no 2 threads could hold a lock for the same object, and yet the HotSpot traces say this is happening all the time. For example Object 0x0a6c1bf8 is locked in 2 threads: "10.129.24.35-1" daemon prio=6 tid=0x2836bad0 nid=0x9d4 runnable [0x2b28f000..0x2b28fc9c] at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) - locked <0x2493f6e0> (a java.net.SocksSocketImpl) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) at ... |
|
javanewbie80 wrote: if i have one static synchronized method in my class, and if one thread enters the static synchronized method, then is it possible for other threads to enter other static method (non synchronized though) of this class? Syncing obtains a single object's lock. A non-static synced method obtains the lock for the object pointed to by "this". A static ... |
Generally, rather than testing, threadB simply tries itself to synchronize on the object. If ThreadA has the monitor threadB will be suspended, queued until ThreadA releases it. You should generally code so that the time speant synchronized is an absolute minimum (time in wait() doesn't count since a waiting thread doesn't own the monitor it's waiting on. There's a Thread.holdsLock() method, ... |
Hi All, I am having a doubt regarding locks and various methods that the thread class provides. I want to know whts the behavior of the yield() method of the thread class incase of object lock. For example, when the sleep() method is called, the thread does not give up on the object lock. The wait() method gives up the object ... |
|
Hi I'm messing around with threads and locks but can't seem to make it work. It's an assignment where I have to write two sorting algorithms and be able to display the sorting process in two panels. Each sorting is implemented with a thread. Now I want to make sure that while sorting an array, that the other sorting algorithm cant ... |
I've had a look at various tutorials but still a bit stuck. I have two threads set to run when called by two different actionlisteners. How can I ensure that, when one thread is running that the other thread can until until the execution of that thread is complete and vice versa? |