473,654 Members | 3,098 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.recei ve(new Task("(boss) write a letter"));
secretary.recei ve(new Task("(boss) clean my desk")); }}

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

class Secretary {
java.util.Array List<Task> tasks = new java.util.Array List<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.prin tln("working on '" + t + "'..");
try { Thread.sleep(10 00); }catch(Interrup tedException e){}
System.out.prin tln(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 971
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.recei ve(new Task("(boss) write a letter"));
secretary.recei ve(new Task("(boss) clean my desk")); }}

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

class Secretary {
java.util.Array List<Task> tasks = new java.util.Array List<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.prin tln("working on '" + t + "'..");
try { Thread.sleep(10 00); }catch(Interrup tedException e){}
System.out.prin tln(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
2321
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 more sophisticated), and what situations might perturb it. Even a hint as to what would be considered normal scheduling might help. The root of our problem is that we observed a normally well-behaved web application suddenly limit itself to a...
7
2700
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 that. Problem is that when program is started many threads are created (see output section), when only two should be running at any time. Can you please help me to identify me where the problem is? Best regards
20
3010
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 delegate inside the UI that I call to update the progress meter. I use the Suspend() and Abort() methods based on button events. I can watch the progress meter increase just fine when the thread is running. When I select Start, I enable the Cancel...
6
23729
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); } The problem is that I don't know how to terminate the thread when my class
13
5072
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 only 1 thread per line Trunk_Thread.ApartmentState = ApartmentState.STA
4
2546
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 thread stops or do I need to add some extra code to avoid Synclock staying on after the thread has been stopped. Thanks Fred
51
54819
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() # This thread is expected to finish within a second
14
6873
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 datagridview on the form. I am using the following code to spawn the worker thread... Thread WorkerThread = new Thread(new ThreadStart(WT_MyFunction)); WorkerThread.IsBackground = true; WorkerThread.Start(); The problem I am having is...I cannot seem...
7
5895
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 a thread exit or an application request exception randomly occurred at the OnReceive method (asynchronous tcp stream reading). I searched all over the internet and found a post posted few years ago. He had the same problem as me, and he said it
34
2778
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
8375
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8707
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8482
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8593
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7306
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4149
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.