You are on page 1of 8

Lab Exercise – 10

Q1. Understanding threads: Make two threads, table2 and table3. table2 will print the table of 2,
as 2 x 1 = 2 to 2 x 10 =20. table3 will do the same for table of 3. Start the two threads. What is the
output?
Ans:
class TablePrinter
{
void printTable(int nu)
{
synchronized(this)
{
for (int i = 1; i <= 10; i++)
System.out.println(nu + " x " + i + " = " + nu * i);
}

}
}
class A implements Runnable
{
TablePrinter t = new TablePrinter(); public void run()
{
t.printTable(2);
}
}

class B implements Runnable


{
TablePrinter t = new TablePrinter(); public void run()
{
t.printTable(3);
}
}

class ThreadSyn1
{
public static void main(String[] args)
{
Thread t1 = new Thread(new A());
Thread t2 = new Thread(new B());
t1.start();
34
t2.start();
}
}

Output

Q2. Avoid racing: What can be done in problem 1 to get an ordered output? Ordered output
means, first get the table of either 2 or 3 and then get the table of the other number. This time
while printing tables, insert a gap of 2 seconds in between.
Ans:
class TablePrinter
{
void printTable(int nu)
{
for (int i = 1; i <= 10; i++)
System.out.println(nu + " x " + i + " = " + nu * i);
}
}
class A implements Runnable
{
TablePrinter t; A(TablePrinter ta)
{
t = ta;
}
public void run()
{
//TablePrinter t = new TablePrinter();
synchronized (t)
{
t.printTable(2);
}
}

35
}
class B implements Runnable
{
TablePrinter t; B(TablePrinter ta)
{
t = ta;
}
public void run()
{
//TablePrinter t = new TablePrinter();
synchronized (t)
{
t.printTable(3);
}
}
}
class ThreadSyn2
{
public static void main(String[] args)
{
// System.out.println("Hello world!");
TablePrinter t = new TablePrinter();
A a = new A(t);
B b = new B(t);
Thread t1 = new Thread(a);
Thread t2 = new Thread(b);
t1.start();
t2.start();
}
}
Output

36
Lab Exercise – 11

Q1. Write a java program that implements a multi-thread application that has three threads. First
thread generates random integer every 1 second and if the value is even, second thread computes
the square of the number and prints. If the value is odd, the third thread will print the value of the
cube of the number.
Ans:
import java.util.Random;
class RandomNumberThread extends Thread
{
public void run()
{
Random random = new Random();
for (int i = 0; i < 10; i++)
{
int randomInteger = random.nextInt(100);
System.out.println("Random Integer generated : " + randomInteger);
if((randomInteger%2) == 0)
{
SquareThread sThread = new SquareThread(randomInteger);
sThread.start();
}
else
{
CubeThread cThread = new CubeThread(randomInteger);
cThread.start();
}
try
{
Thread.sleep(1000);
}
;
catch (InterruptedException ex)
{
System.out.println(ex);
}
}
}
}
class SquareThread extends Thread
{
int number;
SquareThread(int randomNumbern)
37
{
number = randomNumbern;
}
public void run()
{
System.out.println("Square of " + number + " = " + (number * number));
}
}
class CubeThread extends Thread
{
int number;
CubeThread(int randomNumber)
{
number = randomNumber;
}
public void run()
{
System.out.println("Cube of " + number + " = " + number * number * number);
}
}
public class MultiThreadingTest
{
public static void main(String args[])
{
RandomNumberThread rnThread = new RandomNumberThread();
rnThread.start();
}
}

Output

38
Q2. Create a list of numbers and then sort them in ascending order as well as in descending order
simultaneously.
Ans:
import java.util.Random;
class RandomNumberThread extends Thread
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Main
{
public static void main(String args[]) throws InterruptedException
{
List<Integer> list = Arrays.asList( 1, 2, 5, 9, 3, 6, 4, 7, 8);
System.out.println("Unsorted List in Java: " + list);
Collections.sort(list);
System.out.println("List in Java sorted in ascending order: " + list);
Collections.sort(list, Collections.reverseOrder());
System.out.println("Java List sorted in descending order: " + list);
ArrayList alphabets = new ArrayList();
alphabets.add("c");
alphabets.add("b");
alphabets.add("a");
alphabets.add("d");
System.out.println("Unsorted ArrayList in Java : " + alphabets);
Collections.sort(alphabets);
System.out.println("Sorted ArrayList in ascending order in Java : " + alphabets);
Collections.sort(alphabets, Collections.reverseOrder());
System.out.println("ArrayList sort in descending order in Java : " + alphabets);
}
}
Output-

Q3. Write a Java program that handles Push operation and Pop operation on stack concurrently.
Ans:
import java.util.Random;
class RandomNumberThread
39
import java.util.*;
class Main{
static void push(Stack st, int a)
{
st.push(new Integer(a));
System.out.println("Element "+a+" push to Stack");
System.out.println("Stack is: " + st);
}
static void pop(Stack st)
{
Integer a = (Integer) st.pop();
System.out.println("Element "+a+" pop to the stack");
System.out.println("Stack is: " + st);
}
public static void main(String[] args)
{
try
{
Stack stack = new Stack();
System.out.println("Stack: "+stack);
push(stack, 12);
push(stack, 15);
push(stack, 32);
push(stack, 54); pop(stack); pop(stack); pop(stack); pop(stack); pop(stack);
}
catch (EmptyStackException ex)
{
System.out.println("Stack is empty");
}
}
}
Output

40
Q4. Write a Java program that first generates a set of random numbers and then determines
negative, positive even, and positive odd numbers concurrently.
Ans:
import java.util.Random
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Main
{
public static void main(String[] args)
{
final int n = 100;
final int half = n / 2;
final Random random = new Random();
List<Integer> numbers = IntStream.range(0, n).map(i -> random.nextInt(n) -
half).boxed().collect(Collectors.toList()); System.out.println(numbers);
AtomicInteger positive = new AtomicInteger(0);
AtomicInteger negative = new AtomicInteger(0);
numbers.parallelStream().peek(i -> {
if (i < 0)
{
negative.incrementAndGet();
}
else if (i > 0)
{
positive.incrementAndGet();
}
}).count();
int zero = n - (positive.get() + negative.get());
System.out.println("Negative: " + negative.get() + ", Zero: " + zero + ", Positive: " + positive.get());
}
}

Output

41

You might also like