This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
i have a class that tests the function of threads.(no object of the class is involved)
a short desc abt the class
the class has: a counter(static, initialized to zero). a synchronized static method accessed by main() thread that increments the counter by one and sleeps for 20 sec.
public synchronized static void setStatic(){ i=i+1; System.out.println("i before sleeping "+i); try{ Thread.sleep(20000);} catch(InterruptedException e){ System.out.println("interrupted"); } System.out.println("i after sleeping "+i);}
what I did is...I opened an ms dos command prompt and I did run that class, so now, the thread has entered the class's synchronized method, increments the counter by 1 and goes to sleep for 20 sec, and by law any thread trying to access this class should need this class's key, which is now with the main() thread that has gone naping some secs ago. mean while I opened another command prompt and I did run the same class (in between that 20 sec).
1.what would be the value of the static variable i in the second ms dos command prompt? will it be 1 or 2?
true or false 1. if it is 1, no matter how many simultaneous run commands you give, only one class will be loaded.
2. if it is 2, the number of classes loaded is equal to the number of commands. that means two classes of same type in same jvm, in my case where i gave two run commands.(hmmm...i'm confused)
If you are running the application two times in separate command windows, you are starting two completely separate Java virtual machines. What's running in the first one does not influence what's running in the second one and vice versa.
You are not running multiple threads inside one JVM; you're starting multiple JVM processes, which are independent of each other.