The company's annual meeting admission two-color ball

package Thread;


import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class AnnualmeetingTest {
public static void main(String[] args) {
Annualmeeting an=new Annualmeeting();
   Thread BackDoor= new Thread(an,"backdoor"); //front door thread
   Thread FrontDoor=new Thread(an,"front door");//backdoor thread
   BackDoor.start();//start backdoor thread
   FrontDoor.start();// start front door thread }
   

}


package Thread;
import java.util.ArrayList;
import java.util.List;
public class Annualmeeting implements Runnable{
/*
* 2. A company organizes an annual meeting. There are two entrances when entering the meeting. You can get a two-color ball lottery ticket. Suppose the company has 100 employees. Use multi-threading to simulate the admission process of the annual meeting,
and count the number of people entering each entrance and the lottery number obtained by each employee. After the thread runs, the printing format is as follows:
The employee numbered: 2 enters the field from the back door! The two-color ball lottery numbers obtained are: [17, 24, 29, 30, 31, 32, 07]
The employee number: 1 enters from the back door Field! The two-color ball lottery numbers obtained are: [06, 11, 14, 22, 29, 32, 15]
//.....Total
of employees who entered from the back door: 13 employees Total of employees who
entered from the front door : 87 employees*/
private int personNumber=100;//Total number of employees
private int BackDoorCount=0;//Count the number of employees entering from the front
private int FrontDoorCount=0;//Count the number of employees entering from the back
List <String> list=new ArrayList<String>();//Record the number of the two-color ball
List<Integer> number=new ArrayList<Integer>();//Record the number of employees who entered the venue
private Object lock=new Object();//同步锁中的对象监视器
//红色球数组
String[] Redarr= {"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17",
"18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33"};
//蓝色球数组
String[] Bluearr= {"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16"}; if( personNumber>0) {//If there are still employees who have not finished enteringsynchronized(lock) {//Add a synchronization lock to the shared data, only one process can come in at a time}}catch(Exception e){if( personNumber>0) {//If there are still employees who have not finished enteringsynchronized(lock) {//Add a synchronization lock to the shared data, only one process can come in at a time}}catch(Exception e){if(personNumber>0) {//If there are still employees who have not finished enteringsynchronized(lock) {//Add synchronization lock to shared data, only one process can come in at a time}}catch(Exception e){Thread.sleep(10);//The thread sleeps for a period of time, so that the two threads can compete to execute more evenly try {list.clear();//Each cycle, clear the number of the collection, you can also put the above List<String > list=new ArrayList<String>(); put it herewhile(true) {public void run() {
@Override













int num=(int)(Math.random()*100+1);//Employees enter randomly, employees from 1-100
if(number.contains(num)) {//If the employee has entered, Then continue
continue;
}else {
number.add(num);//Add new employees to the set
}
for (int i = 0; i < 6; ) {//Generate 6 red ball numbers
int j=(int )(Math.random()*33);//Randomly generate numbers from 0-32
if(list.contains(Redarr[j])) {//If the set already contains the number, continue, the same goes for the following
continue ;
}else {
list.add(Redarr[j]);
i++;
}
}
for (int i = 0; i < 1; ) {//Generate a blue ball number
int j=(int)(Math.random ()*16);
if(list.contains(Bluearr[j])) {
continue;
}else {
list.add(Bluearr[j]);
i++;
}
}
if(Thread.currentThread().getName(). equals("Front door")) {//Statistics from which door to enter
BackDoorCount++;
}else {
FrontDoorCount++;
}
System.out.println("The employee number: "+num+" enters the field from "+Thread.currentThread().getName()+"! The two-color ball lottery number obtained is:" +list);
personNumber--; //The above code is executed once, which proves that a person has entered the venue, so personNumber-- }else { //The content of the else should also be judged by the thread, and finally add the break statement, otherwise The if(Thread.currentThread().getName().equals("Front Door")) that the front and rear door threads will execute once more { System.out.println("The total number of employees entering from the front door: "+FrontDoorCount); break; } if(Thread.currentThread().getName().equals("Backdoor")) { System.out.println("Total number of employees entering from the backdoor:"+BackDoorCount); break; } } } } } }

















Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325993514&siteId=291194637