一些線程相關的問題

題目:

       8個運動員同時起跑,都到達終點後記錄成績

實現:

package com.spring.test;

import java.util.Random;
import java.util.concurrent.CountDownLatch;

/**
 * Created by brady on 15-3-1.
 */
public class CountDownLatchTest implements Runnable{
    private int id;
    private CountDownLatch beginSingnal;
    private CountDownLatch endSingnal;
    public CountDownLatchTest(int id ,CountDownLatch beginSingnal,CountDownLatch endSingnal){
        this.id = id;
        this.beginSingnal = beginSingnal;
        this.endSingnal = endSingnal;
    }
    @Override
    public void run(){
        try{
            beginSingnal.await();
            Random random = new Random();
            Thread.sleep(Math.abs(random.nextInt())%100);
            endSingnal.countDown();
            System.out.println("我到了:" + this.id);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args){
        CountDownLatch beginSingnal = new CountDownLatch(1);
        CountDownLatch endSingnal = new CountDownLatch(8);
        for(int i=1;i<=8;i++){
            new Thread(new CountDownLatchTest(i,beginSingnal,endSingnal)).start();
        }
        beginSingnal.countDown();
        try {
            endSingnal.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}


兩個線程循環打印

package com.spring.test;

/**
 * Created by brady on 15-3-13.
 */
/*
 * 兩個線程循環打印
 */
public class WaitNotify {
    private static final Object a = new Object();
    private static boolean x1 = true;
    private static boolean y1 = false;

    private static final int x = 30;
    private static int y = 1;
    public static void main(String[] args){
        Thread t1 = new Thread(){
            public void run(){
                synchronized (a){
                    while (!x1) try {
                        a.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    while (y<x){
                        for(int i=0;i<3;i++){
                            System.out.println("線程一:" + y++);
                        }
                        x1=false;
                        y1=true;
                        a.notify();
                    }

                }
            }
        };
        Thread t2 = new Thread(){
            public void run(){
                synchronized (a){
                    while (!y1) try {
                        a.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    while (y<x){
                        for(int i=0;i<3;i++){
                            System.out.println("線程二:" + y++);
                        }
                        y1=false;
                        x1=true;
                        a.notify();
                    }

                }

            }
        };
        t1.start();
        t2.start();
    }
}


發佈了79 篇原創文章 · 獲贊 9 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章