Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

two simultaneous run commands on same class.  RSS feed

 
krishna prasad gunasekaran
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15859
80
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chris Beckey
Ranch Hand
Posts: 116
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}

}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!