同时运行线程

时间:2014-02-14 01:10:49

标签: java multithreading synchronization

我有下一个代码:

    public class App 
{
    public static void main( String[] args )
    {
        NewSmartThread n = new NewSmartThread("ST");
        try{
            n.join();
        }catch(InterruptedException e){
            System.out.println( "Умный поток был прерван" );
        }
        System.out.println( "Hello World!" );
    }
}

class NewSimpleThread extends Thread {

    public NewSimpleThread(String name){
        super(name);
    }

    public void run(){
            try {
                for(int i =1; i<6; i++)
                {
                    System.out.println("Thread " + this.getName() + " : " + i);
                    Thread.sleep(500);
                }
            } catch (InterruptedException e) {
                System.out.println("Thread " + this.getName() + "был прерван.");
            }
        System.out.println("Thread " + this.getName() + " closed.");
}
}

class NewSmartThread extends Thread {
    //final CountDownLatch start = new CountDownLatch(1);
    //final CountDownLatch finish = new CountDownLatch(2);

    public NewSmartThread(String name) {
        super(name);
        start();
    }

    public void run(){
        //s1.split("(?<=\\G..)")

        NewSimpleThread n1 = new NewSimpleThread("1");
        NewSimpleThread n2 = new NewSimpleThread("2");
        NewSimpleThread n3 = new NewSimpleThread("3");

        n1.start();
        n2.start();
        n3.start();
        try{

            Thread.sleep(3000);


            n1.join();
            n2.join();
            n3.join();
        }catch(InterruptedException e){
            System.out.println("Thread " + "broke.");
        }
        System.out.println("Smart thread " + this.getName() + " closed.");
    }
}

我有下一个结果:

>     Thread 2 : 1
>     Thread 3 : 1
>     Thread 1 : 1
>     Thread 3 : 2
>     Thread 1 : 2
>     Thread 2 : 2
>     Thread 2 : 3
>     Thread 1 : 3
>     Thread 3 : 3
>     Thread 2 : 4
>     Thread 1 : 4
>     Thread 3 : 4
>     Thread 1 : 5
>     Thread 2 : 5
>     Thread 3 : 5
>     Thread 1 closed.
>     Thread 2 closed.
>     Thread 3 closed.
>     Smart thread ST closed.
>     Hello World!

但是如果我在下一个代码中使用block synchronized:

public class App 
{
    public static void main( String[] args )
    {
        NewSmartThread n = new NewSmartThread("ST");
        try{
            n.join();
        }catch(InterruptedException e){
            System.out.println( "Умный поток был прерван" );
        }
        System.out.println( "Hello World!" );
    }
}

class NewSimpleThread extends Thread {
    private static final Object monitor = new Object();
    public NewSimpleThread(String name){
        super(name);
    }

    public void run(){
        synchronized (monitor) {
            try {
                monitor.wait();
                for(int i =1; i<6; i++)
                {
                    System.out.println("Thread " + this.getName() + " : " + i);
                    Thread.sleep(500);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        System.out.println("Thread " + this.getName() + " closed.");
        }
    }

    public static void doNotifyAll(){
        synchronized (monitor) {
            monitor.notifyAll();
        }
    }
}
class NewSmartThread extends Thread {

    public NewSmartThread(String name) {
        super(name);
        start();
    }

    public void run(){
        NewSimpleThread n1 = new NewSimpleThread("1");
        NewSimpleThread n2 = new NewSimpleThread("2");
        NewSimpleThread n3 = new NewSimpleThread("3");

        n1.start();
        n2.start();
        n3.start();
        try{

            Thread.sleep(3000);
            NewSimpleThread.doNotifyAll();

            n1.join();
            n2.join();
            n3.join();
        }catch(InterruptedException e){
            System.out.println("Thread " + "broke.");
        }
        System.out.println("Smart thread " + this.getName() + " closed.");
    }
}

得到下一个结果:

Thread 3 : 1 
Thread 3 : 2 
Thread 3 : 3 
Thread 3 : 4 
Thread 3 : 5 
Thread 3 closed. 
Thread 2 : 1 
Thread 2 : 2 
Thread 2 : 3 
Thread 2 : 4 
Thread 2 : 5 
Thread 2 closed. 
Thread 1 : 1 
Thread 1 : 2 
Thread 1 : 3 
Thread 1 : 4 
Thread 1 : 5 
Thread 1 closed. 
Smart thread ST closed. 
Hello World!

但我需要像第一个例子中那样的结果。并且不能使用像CountDownLatch这样的功能 - 只有同步块(这是实验室实验,这是一个要求)

有谁知道怎么做?

2 个答案:

答案 0 :(得分:2)

如果我正确理解了这个问题,那么问题在于你在第二个例子中如何使用synchronized块。

你有synchronized整个逻辑,包括同步块内的for循环。虽然正确的方法是保持

synchronized(monitor){
    monitor.wait(); //excluding exception handling
}

//for loop logic

为什么?

因为你只是希望线程等待主线程的点头开始工作,一旦他们收到通知你不希望逻辑以synchronized的方式执行,所以释放显示器。因为在for循环时保持监视器,所以没有其他线程能够继续进行,直到持有它的那个完成。

答案 1 :(得分:0)

感谢Narendra Pathai。

我的代码的结束变体是下一个:

public class App 
{
    public static void main( String[] args )
    {
        NewSmartThread n = new NewSmartThread("ST");
        try{
            n.join();
        }catch(InterruptedException e){
            System.out.println( "Умный поток был прерван" );
        }
        System.out.println( "Hello World!" );
    }
}

class NewSimpleThread extends Thread {
    private static final Object monitor = new Object();
    public NewSimpleThread(String name){
        super(name);
    }

    public void run(){
        try {
            Latch.awaitZero();
            for(int i =1; i<6; i++)
            {
                System.out.println("Thread " + this.getName() + " : " + i);
                Thread.sleep(500);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Thread " + this.getName() + " closed.");
    }

    public static void doNotifyAll(){
        synchronized (monitor) {
            monitor.notifyAll();
        }
    }
}
class NewSmartThread extends Thread {

    public NewSmartThread(String name) {
        super(name);
        start();
    }

    public void run(){
        NewSimpleThread n1 = new NewSimpleThread("1");
        NewSimpleThread n2 = new NewSimpleThread("2");
        NewSimpleThread n3 = new NewSimpleThread("3");

        n1.start();
        n2.start();
        n3.start();
        try{

            Thread.sleep(3000);
            Latch.doNotifyAll();

            n1.join();
            n2.join();
            n3.join();
        }catch(InterruptedException e){
            System.out.println("Thread " + "broke.");
        }
        System.out.println("Smart thread " + this.getName() + " closed.");
    }
}

class Latch {
    private static final Object synchObj = new Object();

    public static void awaitZero() throws InterruptedException {
        synchronized (synchObj) {
                synchObj.wait();
        }
    }
    public static void doNotifyAll() {
        synchronized (synchObj) {
                synchObj.notifyAll();
        }
    }
}

我添加了课程:

class Latch {
    private static final Object synchObj = new Object();

    public static void awaitZero() throws InterruptedException {
        synchronized (synchObj) {
                synchObj.wait();
        }
    }
    public static void doNotifyAll() {
        synchronized (synchObj) {
                synchObj.notifyAll();
        }
    }
}

代替

class NewSimpleThread extends Thread {
    private static final Object monitor = new Object();