Symantec Interview Question
Dev LeadsCountry: India
Just want to solve using high level wait() and notify()
package com.careercup.main;
public class Counter {
public static class Thread1 extends Thread {
private final Object lock;
Thread1(Object lock) {
this.lock = lock;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
counter++;
}
System.out.println("Thread1 incremented counter to :"+counter);
synchronized (lock) {
lock.notifyAll();
}
}
}
public static class Thread2 extends Thread {
private final Object lock;
Thread2(Object lock) {
this.lock = lock;
}
@Override
public void run() {
synchronized (this) {
try {
System.out.println("Thread2 waiting");
this.wait();
for (int i = 0; i < 60; i++) {
counter++;
}
System.out.println("Thread2 incremented counter to :"+counter);
synchronized (lock) {
lock.notifyAll();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static class Thread3 extends Thread {
@Override
public void run() {
synchronized(this){
try {
System.out.println("Thread3 waiting");
this.wait();
for (int i = 0; i < 20; i++) {
counter++;
}
System.out.println("Thread3 incremented counter to :"+counter);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static int counter = 0;
public static void main(String[] args) {
Thread thread3 = new Thread3();
Thread thread2 = new Thread2(thread3);
Thread thread1 = new Thread1(thread2);
thread1.start();
thread2.start();
thread3.start();
try {
thread1.join();
thread2.join();
thread3.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package com.example.test;
public class ThreadTest {
public static void main(String[] args) {
ThreadCounter counter = new ThreadCounter();
Thread t1 = new Thread(counter, "T1");
Thread t2 = new Thread(counter, "T2");
Thread t3 = new Thread(counter, "T3");
t1.start();
t2.start();
t3.start();
}
}
class ThreadCounter extends Thread{
public static int counter = 1;
@Override
synchronized public void run() {
int n=20;
if(Thread.currentThread().getName().equals("T2")){
n = 60;
}
for(int i=0;i<n;i++){
System.out.print("Running thread: "+Thread.currentThread().getName()+" ");
System.out.println(counter);
counter++;
}
}
}
package com.example.test;
public class ThreadTest {
public static void main(String[] args) {
ThreadCounter counter = new ThreadCounter();
Thread t1 = new Thread(counter, "T1");
Thread t2 = new Thread(counter, "T2");
Thread t3 = new Thread(counter, "T3");
t1.start();
t2.start();
t3.start();
}
}
class ThreadCounter extends Thread{
public static int counter = 1;
@Override
synchronized public void run() {
int n=20;
if(Thread.currentThread().getName().equals("T2")){
n = 60;
}
for(int i=0;i<n;i++){
System.out.print("Running thread: "+Thread.currentThread().getName()+" ");
System.out.println(counter);
counter++;
}
}
}
package com.example.demo;
public class ThreadTest {
public static void main(String[] args) {
ThreadCounter counter = new ThreadCounter();
Thread t1 = new Thread(counter, "T1");
Thread t2 = new Thread(counter, "T2");
Thread t3 = new Thread(counter, "T3");
t1.start();
t2.start();
t3.start();
}
}
class ThreadCounter extends Thread {
public static int counter = 1;
public static String currentThread = "T1";
int n = 20;
@Override
synchronized public void run() {
while (counter < 100) {
if (!Thread.currentThread().getName().equals(currentThread)) {
try {
wait();
} catch (InterruptedException e) {
}
} else {
for (int i = 0; i < n; i++) {
System.out.print("Running thread: " + Thread.currentThread().getName() + " ");
System.out.println(counter);
counter++;
}
if (Thread.currentThread().getName().equals("T1")) {
currentThread = "T2";
n = 60;
} else if (Thread.currentThread().getName().equals("T2")) {
currentThread = "T3";
n = 20;
}
notifyAll();
}
}
}
}
- Alex December 13, 2017