package test.codechef;
import java.util.*;
import java.io.*;
class abc {
public static void main(String[] args) throws Exception {
ThreadOne th1 = new ThreadOne();
Thread th2 = new Thread(new ThreadTwo());
//CODE TO RUN THE TWO THREADS TOGETHER.
th1.start();
th2.start();
//wait 10 seconds
Thread.sleep(10000);
System.out.println("\n10 seconds donee...........");
System.out.println("Stopping first thread.");
//stop 1st thread
th1.stopthread();
//wait 5 seconds more...
Thread.sleep(5000);
System.out.println("Stopping second thread.");
th2.stop();
//CODE TO RUN 2ND THREAD AFTER FIRST ONE FINISHES.
// System.out.println("Starting 1st thread...");
// th1.start();
// th1.join();
// System.out.println("Staring 2nd thread...");
// th2.start();
// th2.join();
//
}
}
class ThreadOne extends Thread {
public void stopthread() {
this.stop();
}
public void run() {
try {
//
System.out.println("Thread Name: " + Thread.currentThread().getName());
System.out.println("Thread ID: " + Thread.currentThread().getId());
System.out.println("Numbers divisible by 5:");
for (int i = 1; i <= 100; i++) {
if (i % 5 == 0) {
Thread.sleep(1000);
System.out.print(i + " ");
}
}
System.out.println("");
} catch (Exception e) {
System.out.println("Exception is caught.");
}
}
}
class ThreadTwo implements Runnable {
public void stopthread() {
Thread.currentThread().stop();
System.out.println("Stopped.");
}
public void run() {
try {
//
System.out.println("Thread Name: " + Thread.currentThread().getName());
System.out.println("Thread ID: " + Thread.currentThread().getId());
System.out.println("Numbers divisible by 3:");
for (int i = 1; i <= 100; i++) {
if (i % 3 == 0) {
Thread.sleep(2000);
System.out.print(i + " ");
}
}
System.out.println("");
} catch (Exception e) {
System.out.println("Exception caught.");
}
}
}
OUTPUT:
1. When both threads are started simultaneously:
Thread Name: Thread-1
Thread Name: Thread-0
Thread ID: 11
Thread ID: 10
Numbers divisible by 3:
Numbers divisible by 5:
5 3 10 15 20 6 25 30 9 35 12 40 45
10 seconds donee...........
Stopping first thread.
15 18 21 Stopping second thread.
2. When 2nd thread starts after first:
Starting 1st thread...
Thread Name: Thread-0
Thread ID: 10
Numbers divisible by 5:
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100
Staring 2nd thread...
Thread Name: Thread-1
Thread ID: 11
Numbers divisible by 3:
3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99
BUILD SUCCESSFUL (total time: 1 minute 26 seconds)
Hope this helps. Best wishes!!!
Please use java Language Part I Implement a thread by extending the Thread class. Your thread...
Please help me to solve this problem with java language!
write a program that runs a thread which prints a timestamp and then waits a second by doing the following: 1. Implement a class that implements the Runnable interface. (1 point) 2. Place the code for your task into the run method of your class. (6 points) a) To get the date and time, construct a Date object. b) To wait a second, use the sleep method of the Thread...
Create two thread classes: ThreadWaiterOne and ThreadWaiterTwo. The thread classes must implement Runnable. In the Run method for ThreadWaiterOne, delay the code for 20 sec and then output to the console "Thread Waiter One is done." The ThreadWaiterTwo class must wait for 5 sec before outputting the console "Thread Waiter Two is done." Finally create ThreadTester that will run the two classes by having them implement Runnable. The thread classes Create two thread classes, Minute.java and Second.java. The Minute.java class...
HW: Create a java application that has two threads by implementing the Runnable interface (aside from the main thread) • Change the names of all threads • Change the priority of all threads (use the priority constants in Thread • Print the numbers from 0 to 9 o One number per one 2 seconds for the first thread College of Computer Operating Systems ( Laboratory Manual Lab o One number per 4 seconds for the second thread o The main...
Problem 2: (8 pts) The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8.,.. Formally, it can be expressed as: fib0-0 fibl-1 fibn-fibn-1+fibn-2 Write a multithreaded program that generates the Fibonacci sequence. This program should work as follows: On the command line, the user will enter the number of Fibonacci numbers that the program is to generate. The program will then create a separate thread that will generate the Fibonacci numbers, placing the sequence in...
1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("test"); } } 1. The code compiles but will not print anything since t does not invoke the run method. 2. The code will not compile since you cannot invoke "this" in a static method. 3. The program compiles, runs, and prints tests on the console. 2. What will the following example...
Please help with Java test questions, I will post the rest of the question in separate post. Question 1 Consider the following program: public class CountDownApp3 { public static void main(String[] args) { Thread t1 = new CountDown(); Thread t2 = new CountDown(); t1.start(); t2.start(); } } Assuming that the CountDown class extends the Thread class, how many threads are used to run this program? a. 3 b. 2 c. 4 d. 1...
1. The first task in this assignment creates the pid manager whose implementation can simply be a single class. Of course, you can create any other classes you might need to implement the pid manager. You may use any data structure of your choice to represent the availability of process identifiers. One strategy adopts Linux’s approach of a bitmap in which a value of 0 at position i indicates that a process id of value i is available and a...
c++
Part 1 Consider using the following Card class as a base class to implement a hierarchy of related classes: class Card { public: Card(); Card (string n) ; virtual bool is_expired() const; virtual void print () const; private: string name; Card:: Card() name = ""; Card: :Card (string n) { name = n; Card::is_expired() return false; } Write definitions for each of the following derived classes. Derived Class Data IDcard ID number CallingCard Card number, PIN Driverlicense Expiration date...
in java please withe comments & check output
Write a program that contains: Client Thread class that send full operation ("1+2", "1-2", "1*2", " 1/2") to CalculaterServer Server Thread class named CalculaterServer, the server create thread to receives the request and decode the operation and call the appropriate method (add: sub, mul div) which are in class named Calculator to do the operation, and then sends back the result to client. Client show the following window: After choosing operation ask...