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.
The moose likes Threads and Synchronization and the fly likes two simultaneous run commands on same class. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "two simultaneous run commands on same class." Watch "two simultaneous run commands on same class." New topic
Author

two simultaneous run commands on same class.

krishna prasad gunasekaran
Ranch Hand

Joined: Jul 25, 2006
Posts: 158
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)

....plz justify your answer...


have a great day,
krishna prasad
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12929
    
    3

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Chris Beckey
Ranch Hand

Joined: Jun 09, 2006
Posts: 116

I think this will show the behavior that you are looking for:

public class ThreadTest
implements Runnable
{
private static int i = 0;

public synchronized static void setStatic()
{
i=i+1;
System.out.println(Thread.currentThread().getName() + "- i before sleeping " + i);
try
{
Thread.sleep(2000);
}
catch(InterruptedException e)
{

System.out.println(Thread.currentThread().getName() + "-interrupted");
}

System.out.println(Thread.currentThread().getName() + "- i after sleeping " + i);
}

public ThreadTest()
{
}

public void run()
{
for(int cycle=0; cycle<5; ++cycle)
ThreadTest.setStatic();
}

/**
* @param args
*/
public static void main(String[] args)
{
Thread t1 = new Thread(new ThreadTest());
t1.setName("Thread1");
Thread t2 = new Thread(new ThreadTest());
t2.setName("Thread2");

t1.start();
t2.start();
}

}
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: two simultaneous run commands on same class.
 
Similar Threads
wait() and sleep() - looking for suggestions
Urgent threading issue
synchronize method call
IllegalMonitorStateException
The static synchronized methods of the same class always block each other as only one lock per class