Static Class Counter (Int Count Void Inc (Count Count+1 ) Int Getcount (Return Count ) )
Static Class Counter (Int Count Void Inc (Count Count+1 ) Int Getcount (Return Count ) )
Q.1 Declare a class ThreadExample which has counter class, as a nested class in your program:
Create several threads, start them all, and wait for all the threads to terminate. Each thread
increments the counter a specified number of times. After all the threads have completed, the
value of the counter is printed out so it can be compared to the correct value. The user specifies
the number of threads and the number of times each thread increments the counter. Print the
final value of the counter, and see whether it is correct.
class Threadexample{
static class Counter {
int count;
void inc() {
count = count+1;
}
int getCount() {
return count;
}
}
while (true) {
/* Get number of threads and number of increments per thread
* from the user. Exit if number of threads is <= 0. */
System.out.println();
System.out.print("How many threads do you want to run (Enter 0 to
end)? ");
int numberOfThreads = in.nextInt();
if (numberOfThreads <= 0)
break;
do {
System.out.println();
System.out.println("How many times should each thread
increment the counter? ");
numberOfIncrements = in.nextInt();
if (numberOfIncrements < 1) {
System.out.println("Number of increments must be
positive.");
numberOfIncrements = 1;
}
} while (numberOfIncrements <= 0);
System.out.println();
System.out.println("Using " + numberOfThreads + " threads.");
System.out.println("Each thread increments the counter "
+ numberOfIncrements + "
times.");
System.out.println();
System.out.println("Working...");
System.out.println();
IncrementerThread[] workers = new
IncrementerThread[numberOfThreads];
counter = new Counter();
for (int i = 0; i < numberOfThreads; i++)
workers[i] = new IncrementerThread();
for (int i = 0; i < numberOfThreads; i++)
workers[i].start();
} // end while
} // end main()
Q. 2 Which integer between 1 and 10000 has the largest number of divisors, and how many
divisors does it have? Write a program to find the answers and print out the results. It is possible
that several integers in this range have the same, maximum number of divisors. Your program
only has to print out one of them. The basic idea is to go through all the integers, keeping track
of the largest number of divisors in the given range. Also, keep track of the integer that had that
number of divisors. Make use of multiple threads and wait till threads die to get result. Use the
following pseudocode
/*** This program finds the number in the range 1 to some maximum that has the
largest number of divisors. It prints that number and the number of divisors
that it has. */
Q.3 Create a class called MyCurrentDate that implements Runnable interface. The MyCurrentDate
class displays the current date and time 10 times, with 100 milliseconds interval - use
sleep() m ethod for this interval.Create a class called MyMain , which contains main() method, in
which 3 instances of MyCurrentDate threads are being run.
The producer’s job is to generate data, put it into the buffer, and start again.
At the same time, the consumer is consuming the data (i.e. removing it from the buffer),
one piece at a time.
Write a multi-process synchronization program to make sure that the producer won’t try to add
data into the buffer if it’s full and that the consumer won’t try to remove data from an empty
buffer. Use following classes in program
PC : the driver class that creates the single Q, Producer, and Consumer.
import java.util.concurrent.Semaphore;
class Q
{
// an item
int item;
// Producer class
class Producer implements Runnable
{
Q q;
Producer(Q q) {
this.q = q;
new Thread(this, "Producer").start();
}
// Consumer class
class Consumer implements Runnable
{
Q q;
Consumer(Q q){
this.q = q;
new Thread(this, "Consumer").start();
}
// Driver class
class PC
{
public static void main(String args[])
{
// creating buffer queue
Q q = new Q();
// starting consumer thread
new Consumer(q);
Q.5 Write a program that has following classes:class Even extends Thread that prints first five even
integers (i.e. 2 4 6 8 10).After printing there should be sleep interval of 1000 msec. class Odd
extends Thread that prints first five odd integers (i.e. 1 3 5 7 9).After printing there should be
sleep interval of 1000 msec. public class TestEvenOdd: In this class creates objects of class Even
and class Odd and invoke threads
Q. 6 Let's say there is only 1 ticket available in train, and two passengers are trying to book that ticket at
same time without synchronization. It might happen that both might end up booking up ticket, though only
ticket was available, which is of course going to create problem.But if synchronization was there only of
them would have been able to book ticket.
class TicketBooking implements Runnable{
int ticketsAvailable=1;
//Let's say system takes some time in booking ticket (here we have taken 1 second time)
try{
Thread.sleep(1000);
}catch(Exception e){}
ticketsAvailable--;
System.out.println("Ticket BOOKED for : "+ Thread.currentThread().getName());
System.out.println("currently ticketsAvailable = "+ticketsAvailable);
}
else{
System.out.println("Ticket NOT BOOKED for : "+ Thread.currentThread().getName());
}
}
public class MyClass {
public static void main(String args[])
{
TicketBooking obj=new TicketBooking();
thread1.start();
thread2.start();
}
}
OUTPUT
Waiting to book ticket for : Passenger1 Thread
Waiting to book ticket for : Passenger2 Thread
Booking ticket for : Passenger1 Thread
Booking ticket for : Passenger2 Thread
Ticket BOOKED for : Passenger1 Thread
currently ticketsAvailable = 0
Ticket BOOKED for : Passenger2 Thread
currently ticketsAvailable = -1