Exercise 07: Multi-threaded Printing of Hello

Last modified by superadmin on 2018-01-12 20:26

Exercise 07: Multi-threaded Printing of Hello

  1. Create an empty Maven project with an archetype program similarly to the previous exercise. Choose "artifactId" to be ex07, and the package name lv.accenture.ex07: 
cd ~/workspace
mvn archetype:create
    -DgroupId=lv.accenture
    -DartifactId=ex07
    -DpackageName=lv.accenture.ex07

Note: Please make sure that there is no directory called ex07 in your workspace before calling the Maven archetype script.

  1. Open a terminal window and run the command: 
cd ~/workspace/ex07
mvn eclipse:eclipse

 and create an Eclipse project in the directory ex07. 

  1. Compile and run from Eclipse a program with source on the FTP server: ftp://10.122.131.15/Bootcamp/Exercises/ex07/ (copy the file TestRunner.java to an appropriate directory under ~/workspace/ex07/src/main/java, to match the package name. 
  2. You should see an output similar to this in your Eclipse console window: 
Hello #0 from R1
Hello #1 from R1
Hello #2 from R1
Hello #3 from R1
Hello #4 from R1
Hello #5 from R1
Hello #6 from R1
Hello #7 from R1
Hello #0 from R2
Hello #0 from R3
Hello #1 from R2
Hello #1 from R3
Hello #2 from R2
  1. It is easy to see that each thread prints 50 Hello's; but each enumerates the Hello's independently, so there are several Hello:s with #0, etc.
  2. Modify the program so that it still has 3 threads printing the total of 150 Hellos, but the printed lines have global enumeration from #0 to #149 - each of the numbers appears once, and no number is skipped. This means that you should have a global counter, which is used and updated by all three threads. (You can decide, if each thread still prints exactly 50 Hellos, or slightly more or less; but all three threads should be involved in printing and counter updating.)
  3. Each Hello is still accompanied with the thread name, which printed it. 

Second part

  1. Consider the following program with 4 threads. Similar to the previous exercise, each "hello" is properly enumerated along with the name of the thread, which printed it. We also synchronize the critical section. 
package lv.accenture.ex07;

public class TestThreads1 {

  public static void main(String[] args) {
    HelloRunner1 r = new HelloRunner1();
    Thread t1 = new Thread(r, "R1");
    Thread t2 = new Thread(r, "R2");
    Thread t3 = new Thread(r, "R3");
    Thread t4 = new Thread(r, "R4");
    t1.start();
    t2.start();
    t3.start();
    t4.start();
  }
}

class HelloRunner1 implements Runnable {
  int count = 0;

  public void run() {
    while (count < 150) {
      synchronized (this) {
        System.out.println("Hello #" + count + " from "
+ Thread.currentThread().getName());
        count++;
      }
    }
  }
}
  1. Can this program print more than 150 hellos? Try this out and explain the behavior (you can write your comment about this in a separate file README.txt and place it directly under the folder ex07. Check in your README.txt along with the code.
Tags:
Created by Kalvis Apsītis on 2008-07-09 08:50
    
This wiki is licensed under a Creative Commons 2.0 license
XWiki Enterprise 6.4 - Documentation