Question

Review the following code examples and improve them (You should keep and improve the multithreading parts)....

Review the following code examples and improve them (You should keep and improve the multithreading parts). Once the code files have been fixed, make them flexible in such a way that the words are provided by a user. In other words, the program asks a user to enter three sets of words which are used to list the words.

public class Called {

void call(String msg) {

System.out.print(" [" + msg);

try {

Thread.sleep(1000);

} catch(InterruptedException e) {

System.out.println("Interrupted");

}

System.out.println("]");

}

}

public class Caller implements Runnable {

String msg;

Called target;

Thread trd;

public Caller(Called tgt, String s) {

target = tgt;

msg = s;

trd = new Thread(this);

}

public void run() {

target.call(msg);

}

}

public class TestSynchronization {

public static void main(String[] args) {

Called target = new Called();

Caller caller01 = new Caller(target, "CIS 314");

Caller caller02 = new Caller(target, "Advanced Programming");

Caller caller03 = new Caller(target, "Java");

caller01.trd.start();

caller02.trd.start();

caller03.trd.start();

try {

caller01.trd.join();

caller02.trd.join();

caller03.trd.join();

} catch(InterruptedException e) {

System.out.println("Interrupted");

}

}

}

The output is supposed to be the following:

[CIS 314]

[Advanced Programming]

[Java]

0 0
Add a comment Improve this question Transcribed image text
Know the answer?
Add Answer to:
Review the following code examples and improve them (You should keep and improve the multithreading parts)....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Given: public class RunMethodExample2 { public void run() { for(int i=1;i<=3;i++) { try{ Thread.sleep(1000); catch(InterruptedException ie)...

    Given: public class RunMethodExample2 { public void run() { for(int i=1;i<=3;i++) { try{ Thread.sleep(1000); catch(InterruptedException ie) { ie.printStackTrace(); System.out.println(i); public static void main(String args[]) { Thread th1 = new Thread(new RunMethodExample(), "th1"); Thread th2 = new Thread(new RunMethodExample(), "th2"); th1.run(); th2.run(); Why could the output NOT be the following?

  • Use the Java implementation of transaction() public void transaction(Account from, Account to, double amount) { mutex...

    Use the Java implementation of transaction() public void transaction(Account from, Account to, double amount) { mutex lock1, lock2; lock1 = get_lock(from); lock2 = get_lock(to); acquire(lock1); acquire(lock2); withdraw(from, amount); deposit(to, amount); release(lock2); release(lock1); } The code does not need to have "Driver" (main method) to execute. Also use "Synchronized() method in Java" which provides exclusive access to an object (i.e., from Account or to Account in the transaction). Could you write the code please? Example of Synchronized() method (not my code):...

  • Exercise 1): take the program “InteractiveCounting” (attached). Your task is to change the implementation of both...

    Exercise 1): take the program “InteractiveCounting” (attached). Your task is to change the implementation of both the “ReadInput” and the “Count” classes. Currently these classes extends Thread. You should now use the Runnable interface to implement the thread. The output of the program should not change. I have also attached the class ThreadType2 to give you a working example on how to implement a thread using the Runnable interfacethe program is here package threadExamples; import java.util.Scanner; public class InteractiveCounting {...

  • In java write a simple 1-room chat server that is compatible with the given client code.

    In java write a simple 1-room chat server that is compatible with the given client code. 9 public class Client private static String addr; private static int port; private static String username; 14 private static iter> currenthriter new AtomicReference>(null); public static void main(String[] args) throws Exception [ addr -args[]; port Integer.parseInt (args[1]); username-args[21 Thread keyboardHandler new Thread(Main: handlekeyboardInput); 18 19 while (true) [ try (Socket socket -new Socket (addr, port) println(" CONNECTED!; Printwriter writer new Printwriter(socket.getoutputStreamO); writer.println(username); writer.flush); currenthriter.set(writer); BufferedReader...

  • 1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System....

    1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("test"); } } 1. The code compiles but will not print anything since t does not invoke the run method. 2. The code will not compile since you cannot invoke "this" in a static method. 3. The program compiles, runs, and prints tests on the console. 2. What will the following example...

  • Can you please fix the error. package com.IST240Apps; import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*;...

    Can you please fix the error. package com.IST240Apps; import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; import java.net.*; public class LinkRotator extends JFrame implements Runnable, ActionListener { String[] pageTitle = new String[5]; URI[] pageLink = new URI[5]; int current = 0; Thread runner; JLabel siteLabel = new Jlabel(); public LinkRotator() { setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE); setSize(300, 100); FlowLayout flo = new Flowlayout(); setLayout(flo); add(siteLabel); pageTitle = new String[] { "Oracle Java Site", "Server Side", "JavaWorld", "Google", "Yahoo", "Penn State" }; pageLink[0] = getUR1("http://www.oracle.com/technetwork/java");...

  • Hello, so I need someone to look over this code and see if im on the...

    Hello, so I need someone to look over this code and see if im on the right path. I feel like im missing something. Here is the assignment: Create a class called Slogans. This class will extend the Thread class. This thread will read from a file several quotes that are on each line. It will pick a quote randomly and display it to the console. It will change quotes every 7 seconds. This will be updated in the final...

  • Consider the following codes: public class TestThread extends Thread {     public static void main(String[] args)...

    Consider the following codes: public class TestThread extends Thread {     public static void main(String[] args) {         TestThread thread = new TestThread();     }     @Override     public void run() {         printMyName();     }     private void printMyName() {         System.out.println("Thread is running");     } } Test Stem / Question Choices 1: What method should you invoke to start the thread TestThread? A: start() B: run() C: No. Thread will run automatically when executed. D: TestThread is not...

  • Question 1 8 pts Write all the different possible outputs of the following Java multi-threaded program:...

    Question 1 8 pts Write all the different possible outputs of the following Java multi-threaded program: class MultithreadingDemo extends Thread public void runot try{ System.out.println (--Multithread.countDown): catch (Exception e) w public class Multithread{ public static int countDown = 10; public static void main(String[] args) { for (int i = 0; i < 3; i++) { MultithreadingDemo object = new MultithreadingDemol); object.start(); } میه

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT