I am looking at improving a package that I believe not to be threadsafe when its input is shared between multiple worker threads. According to TDD principles, I should write ... |
I'm going to reuse OGNL library out of Struts2 scope. I have rather large set of formulas, that is why I would like to precompile all of them:
Ognl.parseExpression(expressionString);
But I'm not sure ... |
Is HttpUrlConnection thread safe? I.e. if I have an HttpConnection instance connected to a server and this instance is used by different threads (e.g.try to send a POST concurrently) how ... |
Are ThreadPoolExecutor and ScheduledThreadPoolExecutor thread-safe??
Now, I have a scenario as under:
- 5
ThreadPoolExecutor(s)
- exec1 (which executes JobA (Job of Level A) : parallelism of 4-5 jobs max),
- exec2 (which executes JobB (made from ...
|
Why is this java class not Thread safe.
class TestClass
{ private int x;
int get(){return x;}
void set(int x){this.x = x;}
}
I read that keyword 'synchronized' ... |
I have been searching for resources to understand thread safety in java and came across this. Is there any popular/comprehensive resource or books that explains thread safety in a ... |
How is the following demo code thread-safe? We are ensuring whether the value is not modified in CAS instruction and then doing an increment on int. Doesn't return v + 1; ... |
|
What are some situations where you need to worry about whether a static method is thread safe?
For example, if I have static utility function that doesn't touch any static class level ... |
Imagine I have this class:
class Notifier {
public Listener listener;
public void notify() {
if (listener != null) ...
|
I have a List, which is to be used either in thread-safe context or in a non thread-safe one. Which one will it be, is impossible to determine in advance.
In such ... |
I have a class C with definition as given below:
public Class C implements Runnable
{
private B ref;
public C(B bobj)
{
...
|
I have a client application that communicates with a server. In this application the clients can send requests to the server in order to reserve hotel rooms.
The problems is that, if ... |
I have many applications writing to my file in concurrent in java.I want to make this operation concurrent and also want my file to preserve order.I have
Thread 1 writing from ... |
Despite me reading wikipedia and such, I still don't really understand what Thread Safety means in a programming sense. Is anyone able to give some Java examples in layman terms? Such ... |
|
I understood that only long and double needed explicit sychronization as they are 64 bit in size considering many of the processors are 32 bit. If this understanding is correct consider this: Class ThreadSafety { private int i; public int getInt() { return i; } public void setInt(int var) { i = var; } // (1) //public sychronized void setInt(int var) ... |
Let's assume there are shared objects or primitives among a group of threads, and there are no synchronization methods nor are these objects known thread-safe classes such as Hashtable/Vector. How much atomicity is there for simple objects or primitives under these circumstances? What kind of thread safey conflicts for reading/writing data can you expect and under what conditions? Ok that's a ... |
I think I'm misunderstanding something very fundamental about method synchronization and thread safety. Here's some example code from Java's String class about what I'm not grasping. Notice that the toUpperCase method is public and NOT synchronized. It calls the upperCaseIndex method which is also not synchronized but IS static. So, this doesn't seem safe to me. Meaning, couldn't two instances of ... |
public class ThreadTest { //Note that I have no class level variables protected void method1(SomeObject myObject) { SomeSubObject subObject = myObject.getSubObject(); String theReturn = method2(subObject); myObject.setReturn(theReturn); } public static String method2(SubObject subObject) { StringBuffer buf = new StringBuffer(64); synchronized (buf) // IS THIS NEEDED? { buf.append((subObject.getValue1()) .append(subObject.getValue2()) .append(' ') .append(subObject.getValue3())); } return buf.toString(); } } |
|
Hi, i have to redesign a class, say X, in such a way that i manage multiple threads accessing a single instance of the class, we cannot create multiple instances of X. The class looks like this: Class X{ boolean isACalled=false; boolean isInitCalled=false; boolean isBCalled=false; A(){ isACalled=true; } Init(){ if(!isACalled) A(); B(); C(); isInitCalled=true; } B(){ if(!isACalled) A(); isBCalled=true; } C(){ ... |
There seems to be an assumption in this thread that "reentrant" is a term restricted to the topic of threading. It isn't. Java locks are reentrant. That means that a thread that already holds the lock can safely enter another block of code, synchronised using the same lock. Some other platforms do not have reentrant locks, and you somehow have to ... |
hi all, this afternoon, my techer give us a piece of code to ask how to make the code threads safety. Code: public class Test { private int b; public void foo(){ int current =b; b= current +1; } public void go(){ for (int i=0;i<5;i++){ new Thread(){ public void run(){ foo(); System.out.println(b+","); } }.start(); } } public static void main(String[] args){ ... |
FileChannel and thread safety (I/O and Streams forum at JavaRanch) A friendly place for programming greenhorns! Register / Login Java Forums Java I/O and Streams FileChannel and thread safety Post by: S Bala, Ranch Hand on Jul 15, 2003 13:49:00 This is Jim Y here, intruding on S Bala's original post. These posts were originally in another thread here, ... |
Hi guys, I'm not sure about the thread safety of one of my classes. Here is some code. import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { ThreadSafety threadSafety = ThreadSafety.getInstance(); threadSafety.doItSafe(); } } class ThreadSafety { private static ThreadSafety threadSafety; private IMustBeSafe iMustBeSafe; private ThreadSafety() { iMustBeSafe = new IMustBeSafe(); } public static ThreadSafety getInstance() ... |
It is NOT thread safe. You are synchronizing an instance method that modifies a static variable. Each instance of the class Test has its own lock ensuring that no 2 threads will access the doit() method on the same single instance of Test simultaneously. But nothing stops somebody from constructing multiple instances of Test and starting all of them up. In ... |
I am learning Thread, so I write an application to test my understanding. Here is a simulation of trading application. The vendor, like Bloomberg, send out the latest market data of a security. My application receives it and save it into queue. And threads will process it one by one. After this action, it will save the updated security into another ... |
|
Hi, I am going through the following url Java theory and practice - IBM Under the section, What do you mean by "publish"? , the following code is written public class Safe { private Object me; private Set set = new HashSet(); private Thread thread; public Safe() { // Safe because "me" is not visible from any other thread me = ... |
|
Threading looks fine too me... the connection is created on a background thread, but (critically) all GUI updates are performed on the EDT, even if there's an exception. Well done. My only comment is... why is your GUI creating a database connection at all? Ideally the controler would get the DAO (which would connect itself) and inject it (the connected DAO) ... |
|
I have an inner class instantiated twice in my main application. Each instantiation instantiates the same extension of class Thread. Next a different extension of Thread is instantiated from the main application, and sleeping and looking at a variable in each of the two mentioned inner classes, waits for data to be ready then does its thing. Im calling methods of ... |
As per the description of volatile:- "The Java programming language provides a second mechanism, volatile fields, that is more convenient than locking for some purposes. A field may be declared volatile, in which case the Java memory model (17) *ensures that all threads see a consistent value for the vari*able." In which case my guess is that Thread 2's getFoo method ... |
Encountered a strange problem when working on a small piece code that is supposed to tally up the number of files, sub-directories and total sizes for a given list of directories using java.io.File. Because the list could be long, the initial version of the code used multiple threads, where each thread responsible for the accounting of a directory off the list. ... |