2
import java.util.logging.Level;
import java.util.logging.Logger;


public class test {

    private static void m1(final String a) {
        Thread t1 = new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                        System.out.print(a);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(test.class.getName()).log(Level.SEVERE,
                                                                   null, ex);
                    }
                }
            });
        t1.start();
    }

    private static void m2(final String a) {
        Thread t2 = new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                        System.out.print(" " + a);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(test.class.getName()).log(Level.SEVERE,
                                                                   null, ex);
                    }
                }
            });
        t2.start();
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            m1("Mihi");
            m2("Cherub");
            System.out.println("");
        }
    }
}

我想像 MihiCherub MihiCherub 同样输出 10 次

但现在我的输出是“MihiMihi Cherub CherubMihi CherubMihiMih”。我想同步我的两种方法,并希望得到 mihicherub 的结果。请帮忙..

4

2 回答 2

2

如果要同步行为,最好的办法是使用一个线程。

线程是为相对独立的任务而设计的,越独立越好。

你可以通过同步、等待和通知来做你想做的事,但这会相对复杂(而且毫无意义)

在您的情况下,println("")将在任何其他 print() 之前执行。这是故意的吗?

于 2012-09-03T07:37:09.857 回答
0

使用CountDownLatch你可以实现这一点,但它不是一种有效的方法。我已将您的代码修改如下。

import java.util.concurrent.CountDownLatch;
import java.util.logging.Level;
import java.util.logging.Logger;

public class test {

private static void m1(final String a, final CountDownLatch sharedCountDownLatch,
        final CountDownLatch masterCountDownLatch,
        final CountDownLatch doneCountDownLatch) {
    Thread t1 = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                sharedCountDownLatch.countDown();
                sharedCountDownLatch.await();
                System.out.print(a);
                masterCountDownLatch.countDown();

            } catch (InterruptedException ex) {
                Logger.getLogger(test.class.getName()).log(Level.SEVERE,
                        null, ex);
            } finally {
                doneCountDownLatch.countDown();
            }
        }
    });
    t1.start();
}

private static void m2(final String a, final CountDownLatch sharedCountDownLatch,
        final CountDownLatch masterCountDownLatch,
        final CountDownLatch doneCountDownLatch) {
    Thread t2 = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                sharedCountDownLatch.countDown();
                sharedCountDownLatch.await();
                masterCountDownLatch.await();
                System.out.print(" " + a + " ");
            } catch (InterruptedException ex) {
                Logger.getLogger(test.class.getName()).log(Level.SEVERE,
                        null, ex);
            } finally {
                doneCountDownLatch.countDown();
            }
        }
    });
    t2.start();
}

public static void main(String[] args) throws InterruptedException {
    for (int i = 0; i < 10; i++) {
        CountDownLatch doneCountDownLatch = new CountDownLatch(2);//CountDownLatch which will be to ensure that both threads executed.
        CountDownLatch sharedCountDownLatch = new CountDownLatch(2);//CountDownLatch that will be shared between both threads so that it will ensure that both threads are at the same execution point 
        CountDownLatch masterCountDownLatch = new CountDownLatch(1);//CountDownLatch which will be used when master thread completes it work
        m1("Mihi", sharedCountDownLatch, masterCountDownLatch,
                doneCountDownLatch);
        m2("Cherub", sharedCountDownLatch, masterCountDownLatch,
                doneCountDownLatch);
        System.out.println("");
        doneCountDownLatch.await();
    }
}
}

输出为:

Mihi Cherub 
Mihi Cherub 
Mihi Cherub 
Mihi Cherub 
Mihi Cherub 
Mihi Cherub 
Mihi Cherub 
Mihi Cherub 
Mihi Cherub 
Mihi Cherub 
于 2012-09-03T10:00:34.877 回答