473,503 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Thread Problem

34 New Member
Hi all,

I have an assignment due tomorrow and I really need your help. I have the program code and according to the assignment I need to change it to make it working according to part (a) and( b). I am really confused how to implement this thread program. Here is the code.


class Task {
String task;
public Task(String task) { this.task = task; }
public String toString() { return task; } }

class Boss {
Secretary secretary;
Boss(Secretary secretary) { this.secretary = secretary; }
void work() {
secretary.receive(new Task("(boss) write a letter"));
secretary.receive(new Task("(boss) clean my desk")); }}

class Student {
Secretary secretary;
Student(Secretary secretary) { this.secretary = secretary; }
void work() {
secretary.receive(new Task("(student) Do my homework"));
secretary.receive(new Task("(student) proof-read my assignment")); } }

class Secretary {
java.util.ArrayList<Task> tasks = new java.util.ArrayList<Task>();
public void receive(Task task) {
tasks.add(task); }

public void goToWork() {
while(true) {
if(tasks.size() > 0) {
Task t = tasks.remove(0);
System.out.println("working on '" + t + "'..");
try { Thread.sleep(1000); }catch(InterruptedException e){}
System.out.println(tasks.size() + " tasks left...");
}
}
}

public static void main(String[] args) {
Secretary jane = new Secretary();
Student john = new Student(jane);
Boss mrDoe = new Boss(jane);

john.work();
mrDoe.work();
jane.goToWork();
john.work();
mrDoe.work();
}
}






(a) Change the code to use separate threads for the secretary and a student and a boss giving her assignments at random intervals. Argue for each method you synchronize, and each place you use a synchronized block. Remember that just because the program does as you expect, it is not a guarantee that it is thread safe.

(b) Enhance the code such that if there are no jobs for the secretary she will wait() for jobs

I really appreciate your help. Looking forward to your reply.

Koonda
Apr 1 '07 #1
1 963
r035198x
13,262 MVP
Hi all,

I have an assignment due tomorrow and I really need your help. I have the program code and according to the assignment I need to change it to make it working according to part (a) and( b). I am really confused how to implement this thread program. Here is the code.


class Task {
String task;
public Task(String task) { this.task = task; }
public String toString() { return task; } }

class Boss {
Secretary secretary;
Boss(Secretary secretary) { this.secretary = secretary; }
void work() {
secretary.receive(new Task("(boss) write a letter"));
secretary.receive(new Task("(boss) clean my desk")); }}

class Student {
Secretary secretary;
Student(Secretary secretary) { this.secretary = secretary; }
void work() {
secretary.receive(new Task("(student) Do my homework"));
secretary.receive(new Task("(student) proof-read my assignment")); } }

class Secretary {
java.util.ArrayList<Task> tasks = new java.util.ArrayList<Task>();
public void receive(Task task) {
tasks.add(task); }

public void goToWork() {
while(true) {
if(tasks.size() > 0) {
Task t = tasks.remove(0);
System.out.println("working on '" + t + "'..");
try { Thread.sleep(1000); }catch(InterruptedException e){}
System.out.println(tasks.size() + " tasks left...");
}
}
}

public static void main(String[] args) {
Secretary jane = new Secretary();
Student john = new Student(jane);
Boss mrDoe = new Boss(jane);

john.work();
mrDoe.work();
jane.goToWork();
john.work();
mrDoe.work();
}
}






(a) Change the code to use separate threads for the secretary and a student and a boss giving her assignments at random intervals. Argue for each method you synchronize, and each place you use a synchronized block. Remember that just because the program does as you expect, it is not a guarantee that it is thread safe.

(b) Enhance the code such that if there are no jobs for the secretary she will wait() for jobs

I really appreciate your help. Looking forward to your reply.

Koonda
You forgot to check the guidelines first.
Apr 2 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
2315
by: Tony Proctor | last post by:
Hi everyone We're experiencing some serious anomalies with the scheduling of ASP threads. I'd be interested to hear if anyone knows what algorithm is used (e.g. simple round-robin, or something...
7
2684
by: Ivan | last post by:
Hi I have following problem: I'm creating two threads who are performing some tasks. When one thread finished I would like to restart her again (e.g. new job). Following example demonstrates...
20
2990
by: Doug Thews | last post by:
I ran into an interesting re-pain delay after calling the Abort() method on a thread, but it only happens the very first time I call it. Every time afterward, there is no delay. I've got a...
6
23706
by: Tomaz Koritnik | last post by:
I have a class that runs one of it's method in another thread. I use Thread object to do this and inside ThreadMethod I have an infinite loop: While (true) { // do something Thread.Sleep(100);...
13
5054
by: Bob Day | last post by:
Using vs2003, vb.net I start a thread, giving it a name before start. Code snippet: 'give each thread a unique name (for later identification) Trunk_Thread.Name = "Trunk_0_Thread" ' allow...
4
2535
by: fred | last post by:
I use a Synclock in a secondary thread and also stop the thread using the abort method. If the abort occurs while the thread is in the Synclock will the SyncLock always be released before the...
51
54769
by: Hans | last post by:
Hi all, Is there a way that the program that created and started a thread also stops it. (My usage is a time-out). E.g. thread = threading.Thread(target=Loop.testLoop) thread.start() ...
14
6848
by: joey.powell | last post by:
I am using VS2005 for a windows forms application. I need to be able to use a worker thread function to offload some processing from the UI thread. The worker thread will need access to a...
7
5880
by: Sin Jeong-hun | last post by:
Hi. I'm writing a Client/Multi-threaded Server program on Windows Vista. It worked fine on Windows Vista, but when the server ran on Windows XP, I/O operation has been aborted because of either...
34
2749
by: Creativ | last post by:
Why does Thread class not support IDisposable? It's creating quite some problem. Namely, it can exhaust the resource and you have not control over it.
0
7198
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7072
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5570
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4998
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
373
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.