Hi, Is it possible for a Thread to access a local variable created by other Thread? For example, Thread x access a method and create a local variable. Thread x has not finished, but then Thread y access the same method and also create a local variable. Is it possible for Thread y to access the local variable created by Thread ...
A method is defined in a class that implements Thread. If a method has local variables, and is called twice, will the 2 invocations share the copies of the local variables? or will the local variables have their own values for each invocation of the method. (I have an understanding that they will have their own local space) When then will ...
I'm not sure if this is even a sensible question. I was wondering if the following code can ever print a 1. public class Mythread extends Thread { public static void main(String[] args){ Mythread a=new Mythread(); Mythread b=new Mythread(); a.start(); b.start(); new Mythread().start(); new Mythread().start(); new Mythread().start(); } public void run(){ try{ line 1:int count=0; Thread.sleep(1000); System.out.println(count); count++; Thread.sleep(1); }catch(InterruptedException ie){ ...
Let's open up Henry's point with a couple examples. Here the local variable map is pretty safe because we create the map locally, use it locally and don't let anybody else see it. public void doSomething() { Map localMap = new HashMap(); do something with the map } But in the next one, we don't know who else might be using ...
There is no thread-safety problem with the original code you posted, at least not associated with the SimpleDateFormat. Each thread makes a separate call to the getTimeStampAsString() method, and each time the method is called it creates a new SimpleDateFormat object. This object is independent for each method call, not shared between them so there is no chance of the object's ...
I'm new at Java but thought that this would be the best place to ask my question. My app will run some threads and I'm testing one thread class now. I'm now confused about which of the class variables are local and which are shared among instances of the thread. In the below heuristic snippet I thought that "secondVariable" would be ...