Java Programming - Threads
- Threads - General Questions
- Threads - Finding the output
- Threads - Pointing out the correct statements
class s1 extends Thread
{
public void run()
{
for(int i = 0; i < 3; i++)
{
System.out.println("A");
System.out.println("B");
}
}
}
class Test120 extends Thread
{
public void run()
{
for(int i = 0; i < 3; i++)
{
System.out.println("C");
System.out.println("D");
}
}
public static void main(String args[])
{
s1 t1 = new s1();
Test120 t2 = new Test120();
t1.start();
t2.start();
}
}
We cannot predict the order in which threads are going to run.
class s implements Runnable
{
int x, y;
public void run()
{
for(int i = 0; i < 1000; i++)
synchronized(this)
{
x = 12;
y = 12;
}
System.out.print(x + " " + y + " ");
}
public static void main(String args[])
{
s run = new s();
Thread t1 = new Thread(run);
Thread t2 = new Thread(run);
t1.start();
t2.start();
}
}
The program will execute without any problems and print 12 12 12 12.
public class ThreadDemo
{
private int count = 1;
public synchronized void doSomething()
{
for (int i = 0; i < 10; i++)
System.out.println(count++);
}
public static void main(String[] args)
{
ThreadDemo demo = new ThreadDemo();
Thread a1 = new A(demo);
Thread a2 = new A(demo);
a1.start();
a2.start();
}
}
class A extends Thread
{
ThreadDemo demo;
public A(ThreadDemo td)
{
demo = td;
}
public void run()
{
demo.doSomething();
}
}
You have two different threads that share one reference to a common object.
The updating and output takes place inside synchronized code.
One thread will run to completion printing the numbers 1-10.
The second thread will then run to completion printing the numbers 11-20.
public class WaitTest
{
public static void main(String [] args)
{
System.out.print("1 ");
synchronized(args)
{
System.out.print("2 ");
try
{
args.wait(); /* Line 11 */
}
catch(InterruptedException e){ }
}
System.out.print("3 ");
}
}
1 and 2 will be printed, but there will be no return from the wait call because no other thread will notify the main thread, so 3 will never be printed. The program is essentially frozen at line 11.
A is incorrect; IllegalMonitorStateException is an unchecked exception so it doesn't have to be dealt with explicitly.
B and C are incorrect; 3 will never be printed, since this program will never terminate because it will wait forever.
public class SyncTest
{
public static void main (String [] args)
{
Thread t = new Thread()
{
Foo f = new Foo();
public void run()
{
f.increase(20);
}
};
t.start();
}
}
class Foo
{
private int data = 23;
public void increase(int amt)
{
int x = data;
data = x + amt;
}
}
and assuming that data must be protected from corruption, what—if anything—can you add to the preceding code to ensure the integrity of data?Option D is correct because synchronizing the code that actually does the increase will protect the code from being accessed by more than one thread at a time.
Option A is incorrect because synchronizing the run() method would stop other threads from running the run() method (a bad idea) but still would not prevent other threads with other runnables from accessing the increase() method.
Option B is incorrect for virtually the same reason as A—synchronizing the code that calls the increase() method does not prevent other code from calling the increase() method.