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(); } } this.bflag = false; if (!mylist.contains(id)) { mylist.add(id); if (position == "qian门" || position == "后门") listposcount.add(position); int nums[] = getnumbers(7); dic.put(id, nums); system.out .println("编号为: " + id + " 的员工 从" + position + " 入场! 拿到的双色球彩票号码是:" + arrays.tostring(nums)); } else { } this.notifyall(); } int a = 0, b = 0; for (string str : listposcount) { if (str == "后门") a++; if (str == "qian门") b++; } system.out.println("从后门入场的员工总共:" + a + " 位员工"); system.out.println("从后门入场的员工总共:" + b + " 位员工"); } } }
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();
}
}
如对本文有疑问, 点击进行留言回复!!
[杭电多校2020]第一场 1004 Distinct Sub-palindromes
Swift -- 将本地生成的UIImage进行持久化保存(存到文件中fileManager.createFile)
网友评论