1.Add code to an existing application. You must determine whether the code is run in a
multithreaded environment, and, if so, make it thread-safe.
Ans https://drive.google.com/open?id=0B-yL1mP3zqTbREthdFpVelpMb0k
2. Create a new project and start a new thread.
Ans package com.crunchify;
public class CrunchifyThread {
/**
* @author Crunchify.com
*/
public static void main(String args[]) {
new ThreadTest("eBay").start();
new ThreadTest("Paypal").start();
new ThreadTest("Google").start();
}
}
class ThreadTest extends Thread {
public ThreadTest(String str) {
super(str);
}
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("Loop " + i + ": " + getName());
try {
sleep((int) (Math.random() * 2000));
} catch (InterruptedException e) {
}
}
System.out.println("Test Finished for: " + getName());
}
}
3. Write a Java program to simulate the table tennis game. The game should be played in a synchronized manner by two players. When the two players are ready, an appropriate message should be displayed. When the game starts, the output should be displayed in the following format: Player 1 ready...
Player 2 ready...
Ping--> <--Pong
Ans https://drive.google.com/open?id=0B-yL1mP3zqTbWjhzbml3RVVHMHc
4. Write a Java program that creates three threads. All three threads should get executed simultaneously and should display the values between 0 and 6. Moreover, you need to ensure that the main thread must be terminated when the execution of the all other threads gets completed. In addition, the output should be displayed in the following format: Thread created: Thread[ChildThread,3,main] Thread created: Thread[ChildThread,7,main] Thread created: Thread[ChildThread,8,main] Main thread waiting for child thread to finish Thread[ChildThread,3,main]loop :1 Thread[ChildThread,7,main]loop :1 Thread[ChildThread,8,main]loop :1 Thread[ChildThread,8,main]loop :2 Thread[ChildThread,7,main]loop :2 Thread[ChildThread,3,main]loop :2 Thread[ChildThread,7,main]loop :3 Thread[ChildThread,3,main]loop :3 Thread[ChildThread,8,main]loop :3 Thread[ChildThread,3,main]loop :4 Thread[ChildThread,7,main]loop :4 Thread[ChildThread,8,main]loop :4 Thread[ChildThread,7,main]loop :5 Thread[ChildThread,3,main]loop :5 Thread[ChildThread,8,main]loop :5 Thread[ChildThread,3,]is alive ? : false Thread[ChildThread,7,]is alive ? : false Thread[ChildThread,8,]is alive ? : false Main Thread is exiting
Ans https://drive.google.com/open?id=0B-yL1mP3zqTbdnRBd3hCMWVTaWs
Ans https://drive.google.com/open?id=0B-yL1mP3zqTbREthdFpVelpMb0k
2. Create a new project and start a new thread.
Ans package com.crunchify;
public class CrunchifyThread {
/**
* @author Crunchify.com
*/
public static void main(String args[]) {
new ThreadTest("eBay").start();
new ThreadTest("Paypal").start();
new ThreadTest("Google").start();
}
}
class ThreadTest extends Thread {
public ThreadTest(String str) {
super(str);
}
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("Loop " + i + ": " + getName());
try {
sleep((int) (Math.random() * 2000));
} catch (InterruptedException e) {
}
}
System.out.println("Test Finished for: " + getName());
}
}
3. Write a Java program to simulate the table tennis game. The game should be played in a synchronized manner by two players. When the two players are ready, an appropriate message should be displayed. When the game starts, the output should be displayed in the following format: Player 1 ready...
Player 2 ready...
Ping--> <--Pong
Ans https://drive.google.com/open?id=0B-yL1mP3zqTbWjhzbml3RVVHMHc
4. Write a Java program that creates three threads. All three threads should get executed simultaneously and should display the values between 0 and 6. Moreover, you need to ensure that the main thread must be terminated when the execution of the all other threads gets completed. In addition, the output should be displayed in the following format: Thread created: Thread[ChildThread,3,main] Thread created: Thread[ChildThread,7,main] Thread created: Thread[ChildThread,8,main] Main thread waiting for child thread to finish Thread[ChildThread,3,main]loop :1 Thread[ChildThread,7,main]loop :1 Thread[ChildThread,8,main]loop :1 Thread[ChildThread,8,main]loop :2 Thread[ChildThread,7,main]loop :2 Thread[ChildThread,3,main]loop :2 Thread[ChildThread,7,main]loop :3 Thread[ChildThread,3,main]loop :3 Thread[ChildThread,8,main]loop :3 Thread[ChildThread,3,main]loop :4 Thread[ChildThread,7,main]loop :4 Thread[ChildThread,8,main]loop :4 Thread[ChildThread,7,main]loop :5 Thread[ChildThread,3,main]loop :5 Thread[ChildThread,8,main]loop :5 Thread[ChildThread,3,]is alive ? : false Thread[ChildThread,7,]is alive ? : false Thread[ChildThread,8,]is alive ? : false Main Thread is exiting
Ans https://drive.google.com/open?id=0B-yL1mP3zqTbdnRBd3hCMWVTaWs
No comments:
Post a Comment