0% found this document useful (0 votes)
6 views6 pages

CCP Answers

The document discusses various concepts related to multithreading, including race conditions, deadlocks, and task execution states. It provides a Java implementation of a restaurant scenario with a chef and multiple customers, utilizing the tryLock() method to manage thread synchronization. Additionally, it outlines the conditions necessary for deadlock to occur and describes the behavior of threads in different states.

Uploaded by

Mark
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

CCP Answers

The document discusses various concepts related to multithreading, including race conditions, deadlocks, and task execution states. It provides a Java implementation of a restaurant scenario with a chef and multiple customers, utilizing the tryLock() method to manage thread synchronization. Additionally, it outlines the conditions necessary for deadlock to occur and describes the behavior of threads in different states.

Uploaded by

Mark
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

CCP Answers

1. You have written an application for processing tasks. In this application,


several threads are created and each thread has its own counter. The
program unable to update the total counter from all the increment across
different threads. Which one of the following problems correctly describes
this situation?
Starvation
Deadlock
Livelock
Race condition

2. Parallel overhead can include factors such as:


processor speed
task startup time
synchronizations
ram size

3. A task is typically a program or program-like set of instructions that is


executed by a ________.
memory
processor
clock
monitor

4. When a join() method is called, the thread is in _______ state.


terminated
new
runnable
waiting

5. Interleaving process between two threads will involve _____.


quantum
race condition
deadlock
timeslice

6. A runnable thread enters the terminated state when it successfully


completes its task.

7. When a thread is first created, the state is new.

8. Waiting state go to the runnable state when notified or their time is up.
9. In Round Robin algorithm, timeslices/quantum are assigned to each
process in equal portion.
10. One thread is waiting for another thread to finish transfer and vice versa.
They are stuck with each other and the program cannot continue. This is
called deadlock.

11. Briefly describe about the FOUR (4) conditions of having deadlock.
Deadlock is the event whereby there is no progress with either party
(threads) as they required each other resources. The conditions of having
deadlock are:
 Serially reusable resources - the processes involved share resources which they use under
mutual exclusion.
 Incremental acquisition - processes hold on to resources already allocated to them while
waiting to acquire additional resources.
 No pre-emption - once acquired by a process, resources cannot be pre-empted (forcibly
withdrawn) but are only released voluntarily.
 Wait-for cycle - a circular chain (or cycle) of processes exists such that each process holds a
resource which its successor in the cycle is waiting to acquire.

12. A restaurant consists of one chef and is required to serve five


customers. All five customers are hungry and wanted to be served
immediately. If unable to be served, the customer will leave the
restaurant.

Implement the following case program using tryLock() method. Below are
three different class implemented in java.

Main.java
public class Main
{
public static void main(String arg[])
{
Chef c = new Chef();
Customer u1 = new Customer(1, c);
Customer u2 = new Customer(2, c);
Customer u3 = new Customer(3, c);
Customer u4 = new Customer(4, c);
Customer u5 = new Customer(5, c);
u1.start();
u2.start();
u3.start();
u4.start();
u5.start();
}
}
Chef.java
import java.util.Random;
public class Chef
{
void visit(Customer use)
{
System.out.println("Customer " + use.id + " visit Chef.");
try
{
Thread.sleep(new Random().nextInt(5) * 1000);
}
catch(Exception e)
{

}
System.out.println("Customer " + use.id + " left.");
}
}

Customer.java
public class Customer extends Thread
{
Chef c;
int id;

public Customer(int id, Chef c)


{
this.id = id;
this.c= c;
}

public void run()


{
c.visit(this);
}
}

Answers

MainClass.java

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MainClass


{
public static void main(String arg[])
{
Lock lock = new ReentrantLock();
Chef ramsay = new Chef();
Customer u1 = new Customer(1, ramsay, lock);
Customer u2 = new Customer(2, ramsay, lock);
Customer u3 = new Customer(3, ramsay, lock);
Customer u4 = new Customer(4, ramsay, lock);
Customer u5 = new Customer(5, ramsay, lock);

u1.start();
u2.start();
u3.start();
u4.start();
u5.start();
}
}

Chef.java

import java.util.Random;

public class Chef


{
void visit(Customer use)
{
System.out.println("Customer " + use.id + "
comes to restaurant.");
try
{
Thread.sleep(new Random().nextInt(5) * 1000);
}
catch(Exception e)
{

}
System.out.println("Customer " + use.id + "
left the restaurant.");
}
}
Customer.java

import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

public class Customer extends Thread


{
Chef c;
int id;
Lock lock;

public Customer(int id, Chef c, Lock lock)


{
this.id = id;
this.lock = lock;
this.c = c;
}

public void run()


{
try
{
if(lock.tryLock(new Random().nextInt(15),
TimeUnit.SECONDS))
{
c.visit(this);
lock.unlock();
}
else
{
System.out.println("Customer " + id + " is
tired of waiting and left the restaurant
immediately.");
}
}
catch(InterruptedException ex)
{
ex.printStackTrace();
}
}
}

You might also like