given the following code:
public class FooBar {
public static volatile ConcurrentHashMap myConfigData = new ConcurrentHashMap();
}
public class UpdaterThread implements Runnable {
run {
//Query the Data ...
|
I'm programming a game in j2me in the client side, that connects to a server written in java. This game is going to have a chat feature, but as a secondary ... |
I see this idiom of initializing instance variables quite a bit
public class Test{
private Long l = 1l;
private MyClass mc = new MyClass();
...
|
I am struggling to understand on java threads work so excuse this rather simple question.
Let's assume I have a program with N threads. Each thread executes the same instructions on ... |
Thread t1= new Thread(new Runnable() {
public void run() {
...
|
I've got the following runnable class.
public class OnesRun implements Runnable {
public int ones = 0;
private int passendNumber;
public OnesRun(int passendNumber) ...
|
Say we started up two threads with the same unsafe object as an argument: UnsafeClass unsafeObject = new UnsafeClass(); new ThreadClass(unsafeObject).start(); new ThreadClass(unsafeObject).start(); Then the two threads call some method in UnsafeObject: public void unsafeMethod( int newValue ) { if(unsafeVariable == newValue) 1 doSomethingWith(unsafeVariable) 2 else { unsafeVariable = newValue; 3 doSomethingElse(unsafeVariable); 4 } } We can think of two threads ... |
|
They're only as safe as you make them, and there are many ways to make them unsafe. For example there's nothing safe about passing one instance variable to two other threads. There's nothing safe about two threads calling a method on your object which happens to reference an instance variable. You have to synchronize the method or use another lock to ... |
|
Local variables are thread-safe, in that no other thread can get at them. You may consider them to be stored on the stack for that thread, though Java programmers should rarely concern themselves about such low-level details. You have to be careful not to read too much into this "thread-safe" nature of local variables. Most importantly, if your local variable is ... |
|
You've got it. Since every call to doGet(req, resp) is a seperate thread, and the variable help_1 is declared as a local variable in the method doGet(req, resp), each thread has it's own help_1 and instance of Helper_1. help_2 is fine (used as in your example) since no threads will be sharing an instance of Helper_1, they each have their own. ... |
Thread safety is easier to understand when looking at bytecode. Even individual lines of code in a Java function rarely correspond to a single bytecode instruction. Pre-empting a thread can happen, at least in theory, between any two bytecode instructions -- even, in some cases, "during" one. It may be unlikely that one thread interrupts another in such a way that ... |
Hi ranchers! I have trouble understanding whether this is thread safe or not...and I hope I'm posting this in the right forum. In some classes or servlets I find it easier to declare variables without instantiating them so I can use them within any method in the class. By doing this though, I worry that if 2 simultaneous connections occur that ... |
|
Be careful about using isThreadSafe="false" when your servlet has instance variables (fields) that maintain persistent data. In particular, note that servlet engines are permitted (but not required) to create multiple servlet instances in such a case and thus instance variables are not necessarily unique. You could still use static fields in such a case, however. |
|
Well, I was hoping to be enlightened with a rule that when applied to instance variables and servlet context attributes resulted in them being not thread safe, yet when the rule was applied to local service method variables it resulted in those variables being thread safe. But if the rule is "that's the way it is", then I guess it's just ... |
|
Hi all, I suspect if my implementation shown below is proper and thread safe. This is the scenario: I have web service with a service exposed "MyServices" with an exposed method "getService()" -- The first code. And also we have our SOAP mappings and the calls to busibness logic reside in "run()" method of MyServicesHandler which is a thread. from the ... |
Hello All ! i dont know how to make a local variable thread safe . my local variable (lMyFormatter) is used like below : private void myMethode() { SimpleDateFormat lMyFormatter= SQLParameter.getSimpleDateFormat(); try { if (pResultSet != null) { while (pResultSet.next()) { ComAR fon = new ComAR(); try { String lTemp = pResultSet.getString(SQLParameter .getToCharStringForResultSetHandler("AAPOL112")); if (lTemp != null) { fon.setSinceDate(lMyFormatter.parse(lTemp)); } } ... |
|
I'm guessing that #2 meant to ask about the "singleton pattern". So the question becomes this: "Suppose I have objects that are thread-safe. If I only create one of these objects, will it be thread-safe?" If you can't answer this for yourself, or if I guessed wrong at the question, then post a follow-up question. |