美文网首页Java
Java multithreading

Java multithreading

作者: JaedenKil | 来源:发表于2018-01-29 18:48 被阅读5次
    import com.sun.org.apache.bcel.internal.generic.NEW;
    import org.junit.jupiter.api.*;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.concurrent.CountDownLatch;
    
    import static org.junit.jupiter.api.Assertions.assertEquals;
    
    public class ThreadTest {
        @BeforeAll
        static void setupBeforeClass() {
            System.out.println("BeforeAll executed.");
            printTimestamp();
        }
    
        @BeforeEach
        void setUp() {
            System.out.println("BeforeEach is executed.");
        }
    
        @Test
        void test01() throws InterruptedException {
            System.out.println("test01");
            System.out.println("Sleep 5 seconds.");
            Thread.sleep(5000);
        }
    
        @Test
        void test02() throws InterruptedException {
            CountDownLatch latch = new CountDownLatch(1);
            System.out.println("test02");
            Thread check = new Thread() {
                public void run() {
                    for (int x = 1; x <= 10; x ++) {
                        try {
                            Thread.sleep(1000);
                            System.out.println("This is the inside thread(" + x + ").");
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    latch.countDown();
                }
            };
            check.start();
    
            System.out.println("Sleep 5 seconds.");
            for (int x = 1; x <= 5; x ++) {
                Thread.sleep(1000);
                System.out.println("This is the outside thread(" + x + ").");
            }
            latch.await();
        }
    
        @AfterEach
        void tearDown() {
            System.out.println("AfterEach executed.");
        }
    
        @AfterAll
        static void tearDownAfterClass() {
            System.out.println("AfterAll executed.");
            printTimestamp();
        }
    
        private static void printTimestamp() {
            Long ts = System.currentTimeMillis();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
            //SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            Date date = new Date(ts);
            System.out.println("Date: " + sdf.format(date));
        }
    }
    
    BeforeAll executed.
    Date: 2018-01-29 18:44:36:910
    BeforeEach is executed.
    test01
    Sleep 5 seconds.
    AfterEach executed.
    BeforeEach is executed.
    test02
    Sleep 5 seconds.
    This is the outside thread(1).
    This is the inside thread(1).
    This is the outside thread(2).
    This is the inside thread(2).
    This is the outside thread(3).
    This is the inside thread(3).
    This is the outside thread(4).
    This is the inside thread(4).
    This is the outside thread(5).
    This is the inside thread(5).
    This is the inside thread(6).
    This is the inside thread(7).
    This is the inside thread(8).
    This is the inside thread(9).
    This is the inside thread(10).
    AfterEach executed.
    AfterAll executed.
    Date: 2018-01-29 18:44:51:977
    
    A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.
    
    A CountDownLatch is initialized with a given count. The await methods block until the current count reaches zero due to invocations of the countDown() method, after which all waiting threads are released and any subsequent invocations of await return immediately. 
    This is a one-shot phenomenon -- the count cannot be reset. If you need a version that resets the count, consider using a CyclicBarrier.
    
    A CountDownLatch is a versatile synchronization tool and can be used for a number of purposes. 
    A CountDownLatch initialized with a count of one serves as a simple on/off latch, or gate: all threads invoking await wait at the gate until it is opened by a thread invoking countDown().
    A CountDownLatch initialized to N can be used to make one thread wait until N threads have completed some action, or some action has been completed N times.
    
    A useful property of a CountDownLatch is that it doesn't require that threads calling countDown wait for the count to reach zero before proceeding, it simply prevents any thread from proceeding past an await until all threads could pass.
    

    相关文章

      网友评论

        本文标题:Java multithreading

        本文链接:https://www.haomeiwen.com/subject/virfzxtx.html