|
|
|
|
|
|
I******y 发帖数: 176 | 1 design a class which provides a lock only if there are no possible deadlocks
解答并没有给出到底什么时候可以acquire lock阿.这个canAcquireResource具体怎么
实现呢
For our solution, we implement a wait / die deadlock prevention scheme.
1 class MyThread extends Thread {
2 long time;
3 ArrayList res = new ArrayList();
4 public ArrayList getRes() { return res; }
5
6 public void run() {
7 /*run infinitely*/
8 time = System.currentTimeMillis();
9 int count = 0;
10 while (true) {
11 if (count < 4) {
12 if (Question.canAcquireResource(this,
13 Question.r[count])) {
14 res.add(Question.r[count]);
15 count++;
16 System.out.println(“Resource: [“ +
17 Question.r[count - 1].getId() + “] acquired by
18 thread: [“ + this.getName() + “]”);
19 try {
20 sleep(1000);
21 } catch (InterruptedException e) {
22 e.printStackTrace();
23 }
24 }
25 }
26 else {
27 this.stop();
28 }
29 }
30 }
31
32 public long getTime() { return time; }
33 public void setRes(ArrayList res) { this.res = res; }
34 MyThread(String name) {
35 super(name);
36 }
37 } |
|
|
|
|
|