Thread Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • koonda
    New Member
    • Oct 2006
    • 34

    #1

    Thread Problem

    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
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by koonda
    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.

    Comment

    Working...