What do you expect the outcome to be when RunThread3 is executed? Compile and run i

Consider the following Java classes:
```
import SomeThread3;
public class RunThreads3
{
public static void main (String[] args)
{
int originalThreadCount = Thread.activeCount( );
for (int i=0; i<10; i++) {
Thread p = new Thread(new SomeThread3());
p.start();
System.out.println("thread count=" +Thread.activeCount( ));
}

while (Thread.activeCount() > originalThreadCount ){
// loop until all child threads have exited.
}
System.out.println("finally, Count = " + SomeThread3.count);
}
}//end class RunThreads3

class SomeThread3 implements Runnable {
static int count=0;

SomeThread3() {
super();
}

public void run() {
update();
}

static public synchronized void update( ){
int myCount = count;

int second = (int)(Math.random( ) * 500);
try {
Thread.sleep(second);
}
catch (InterruptedException e) {
}

myCount++;
count = myCount;
System.out.println("count="+count+
"; thread count=" + Thread.activeCount( ));
}
} //end class SomeThread3
```


A synchronized method can only be run by one process/thread at a time, hence the ten child threads spawned by RunThreads3 will run the method consecutively, one at a time. The static variable count is therefore incremented successively, with its value changing from 0 to 10.

Here’s the outcome of a run on a system that runs Java 1.4. Note that the initial active thread count is 3.

Computer Science & Information Technology

You might also like to view...

An object's ____ thus consists of ____ values.

A. position; three B. point of view; three C. position; six D. point of view; six

Computer Science & Information Technology

Describe a T1 line and briefly describe how it is applied in a T1 connection.

What will be an ideal response?

Computer Science & Information Technology

Why are security infrastructure mismatches one of the most serious issues in software security?

What will be an ideal response?

Computer Science & Information Technology

What will be the values of x aand y after the following code is executed?

``` int x = 12, y = 5; x += y--; ``` a. x = 12, y = 5 b. x = 16, y = 4 c. x = 17, y = 5 d. x = 17, y = 4

Computer Science & Information Technology