Deprecated Garbage Collections – Kenny Lee Chee Wei

A truckload of garbage by Kenny Lee Chee Wei

Collaborating threads using CyclicBarrier (Java 5)

with 6 comments

Recently, i came across this problem.

Write a java program that prints out the line “Started” to the console and then creates 10 threads. Each thread should print the lines “Thread x started”, “Thread x finished” where x is the thread number (0 to 9). The program should then print the line “Terminated”. No thread should print the finish message until all threads have printed the started message. The final “Terminated” message should be printed as soon after the last finished message has been printed.

The interesting thing is all the threads have to wait for each other to reach a common point before being allowed to terminate. This common point is known as the CyclicBarrier in java speak.

I did some quick research and came across this class under the java.util.concurrent package. So using this class i managed to complete this problem in about 1+ hrs (Includes refreshing my memory of threads =D).

Full Source Code (Server and ServerThread)

Advertisements

Written by kennii

August 7, 2007 at 7:02 am

Posted in Java

6 Responses

Subscribe to comments with RSS.

  1. The code doesn’t compile in Java 5 …
    s.threadPool.execute(t);
    t is not Runnable.

    Sidney

    April 4, 2008 at 12:24 am

  2. It should compile under Java 5.

    Check outhttp://java.sun.com/j2se/1.5.0/docs/guide/concurrency/index.html

    Kenny Lee

    April 4, 2008 at 3:06 pm

  3. It should compile under Java 5.

    Check out http://java.sun.com/j2se/1.5.0/docs/guide/concurrency/index.html

    Kenny Lee

    April 4, 2008 at 3:07 pm

  4. Is serverThread supposed to have a superclass?
    I see the line ‘super.setName()’ but setName() is not a part of the Object type.

    Sidney

    April 4, 2008 at 3:36 pm

  5. I believe serverThread is intended to extend the Thread class.

    When I added ‘extends Thread’ to ServerThread, it compiled and ran as intended.

    Sidney

    April 4, 2008 at 3:45 pm

  6. необыкновенный у вас сайт! Бесспорно полезные обсуждения интересных мне тем. Добавлю ваш сайт в закладки и буду почаще заходить. Честно говоря не ожидал, млин настолько здесь засяду!
    Zaharov.Ax

    zaharovax

    December 27, 2011 at 2:49 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: