Java multi-threaded practice

Title: A company organized the annual meeting, there are two entrances when meeting admission, each employee can get a color ball lottery admission, assuming that the company has 100 employees, will take advantage of multi-threading simulation admission process,
Count the number of each entry and each entry, as well as lottery numbers each employee get. After the thread running print format is as follows:
Number: 2 staff entrance from the back door to get a color ball lottery number is:! [17, 24, 29, 30, 31, 32, 07]
Number: 1 employee entrance from the back door to get a color ball lottery number is:! [06, 11, 14, 22, 29, 32, 15]
//.....
From the back door entrance of a total staff: 13 employees
Total front door admission staff: 87 employees
 
Analysis: two entrances correspond to two threads, method to get the lottery run method override Runnable interface to achieve, I put this class that implements the Runnable interface called Paper category, which recorded number of employees with id field and provide get, set method. Set of three threads as random controls 100 employees which entry in and out, because it is the employees into the entrance, the entrance was the appropriate lottery method, so I used to wait for wake-up mechanism ( Here we must note that this wait wake-up mechanism must be synchronized blocks in, but also with the same lock.)
 
Code:
class Paper implements Runnable {

    boolean bFlag = false;

    private int id;

    public void setId(int id) {

        this.id = id;
    }

    public int getId() {
        return this.id;
    }

    public String position;

    private List<String> listPosCount = new ArrayList<String>();

    private HashMap<Integer, int[]> dic = new HashMap<Integer, int[]>();

    private List<Integer> myList = new ArrayList<Integer>();

    private int[] getNumbers(int size) {

        int numbers[] = new int[size];

        for (int i = 0; i < size;) {

            boolean flag = false;

            numbers[i] = (int) (Math.random() * 100);

            for (int j = 0; j < i; j++) {

                if (numbers[j] == numbers[i]) {

                    flag = true;

                    break;

                }
            }

            if (flag == true) {

                continue;
            }

            i++;
        }
        return numbers;
    }

    @Override
    public void run() {
        synchronized (this) {
            while (myList.size() < 100) {

                while (!bFlag) {

                    try {

                        this.wait();

                    } catch (InterruptedException e) {

                        e.printStackTrace (); 
                    } 
                }         
            
                the this .bFlag = to false ; 
                
                IF (! {myList.contains (ID)) 

                    myList.add (ID); 
                    IF (position == "Qian door" || position == "back door" ) 
                        listPosCount.add (position); 

                    int the nums [] = getNumbers (. 7 ); 

                    dic.put (ID, the nums); 

                    the System.out 
                            .println ( "number:" + id + "staff from" + position + "into ! games to get a color ball lottery number is: "+ Arrays.toString (nums));

                } else {

                    
                } 
                The this .notifyAll (); 
            } 

            int A = 0, B = 0 ;
             for (String STR: listPosCount) { 

                IF (STR == "back door" ) 
                    A ++ ;
                 IF (STR == "Qian door" ) 
                    B + + ; 

            } 
            System.out.println ( "back door entrance staff total:" + a + "employees" ); 
            System.out.println ( "back door entrance staff total:" + b + "employees " ); 

        } 
    } 
}
//--------------------------------------------------------------------------------
// main function test
public class ThreadLearn2 {

	public static void main(String[] args) {

		Paper p = new Paper();

		Thread th1 = new Thread(p);
		Thread th2 = new Thread(p);

		Runnable run1 = new Runnable() {

			@Override
			public void run() {
				synchronized (p) {
					
				
					List<Integer> list = new ArrayList<Integer>();

					while (list.size() < 100) {
						int i = (int) (Math.random() * 100);

						if (list.indexOf(i) == -1) {

							p.setId(i);

							list.add(i);

							p.bFlag = true;

							int num = (int) (Math.random() * 100) % 2;

							if (num == 1) {

								p.position = "后门";
							} else {
								p.position = "qian门";

							}
							p.notifyAll();
							
							while (p.bFlag) {

								try {

									p.wait();

								} catch (InterruptedException e) {

									e.printStackTrace();
								}
							}

						}

					}

				}
			}

		};

		Thread th3 = new Thread(run1);

		th1.start();
		th2.start();
		th3.start();

	}

}

  

 
 

Guess you like

Origin www.cnblogs.com/HelloQLQ/p/11622162.html