50,530
社区成员
发帖
与我相关
我的任务
分享
import java.util.concurrent.atomic.AtomicIntegerArray;
public class CS extends Thread {
private int id;
AtomicIntegerArray ticket;
AtomicIntegerArray entering;
public CS(int id) {
this.id = id;
ticket = new AtomicIntegerArray(id);
entering = new AtomicIntegerArray(id);
for (int i = 0; i < id; ++i) {
ticket.set(i, 0);
entering.set(i, 0);
}
}
public void run() {
while (true) {
critical_section();
}
}
public void critical_section() {
for (int i = 0; i < id; i++) {
entering.set(i, i);
ticket.set(i, max(entering) + 1);
entering.set(i, 0);
for (int j = 0; j < ticket.length(); ++j) {
if (j != i) {
while (entering.get(i) != 0) {
}
while (ticket.get(j) != 0
&& (ticket.get(j) < ticket.get(i) || (ticket.get(j) == ticket
.get(i) && j < i))) {
}
}
}
System.out.println("Process : " + this.id
+ " is in its critical section" + "get ticket: "
+ ticket.get(i) + " ticket length: " + ticket.length());
unlock(i);
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void unlock(int i) {
ticket.set(i, 0);
non_critical_section();
}
public void non_critical_section() {
System.out.println("Process : " + this.id
+ " is in its non-critical section");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private int max(AtomicIntegerArray turn2) {
// TODO Auto-generated method stub
int max = turn2.get(0);
for (int i = 0; i < this.id; i++) {
if (turn2.get(i) > max) {
max = turn2.get(i);
}
}
return max;
}
}
import java.util.concurrent.atomic.AtomicIntegerArray;
public class CS extends Thread {
private int id;
static AtomicIntegerArray entering = new AtomicIntegerArray(4);;
static AtomicIntegerArray ticket = new AtomicIntegerArray(4);
public CS(int id) {
this.id = id;
}
public void run() {
while (true) {
critical_section(id);
}
}
public void critical_section(int i) {
entering.set(i, 1);
ticket.set(i, max(ticket) + 1);
entering.set(i, 0);
for (int j = 0; j < ticket.length(); ++j) {
if (j != i) {
while (entering.get(i) != 0) {
}
while (ticket.get(j) != 0
&& (ticket.get(j) < ticket.get(i) || (ticket.get(j) == ticket
.get(i) && j < i))) {
}
}
}
System.out.println("Process : " + this.id
+ " is in its critical section" + "get ticket: "
+ ticket.get(i) + " ticket length: " + ticket.length());
try {
unlock(i);
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void unlock(int i) {
ticket.set(i, 0);
non_critical_section();
}
public void non_critical_section() {
System.out.println("Process : " + this.id
+ " is in its non-critical section");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private int max(AtomicIntegerArray turn2) {
// TODO Auto-generated method stub
int max = turn2.get(0);
for (int i = 0; i < this.id; i++) {
if (turn2.get(i) > max) {
max = turn2.get(i);
}
}
return max;
}
}
public class GreatBakeOff {
public static void main(String[] args) {
int processes = 4;
CS[] cs = new CS[processes];
for(int i = 0; i < processes; i++){
cs[i] = new CS(i);
}
for(int i = 0; i < processes; i++){
new Thread(cs[i]).start();
}
}
}
public class GreatBakeOff {
public static void main(String[] args) {
CS cs = new CS(4);
cs.start();
}
}
Process : 4 is in its critical sectionget ticket: 1 ticket length: 4
Process : 4 is in its non-critical section
Process : 4 is in its critical sectionget ticket: 2 ticket length: 4
Process : 4 is in its non-critical section
Process : 4 is in its critical sectionget ticket: 3 ticket length: 4
Process : 4 is in its non-critical section
Process : 4 is in its critical sectionget ticket: 4 ticket length: 4
Process : 4 is in its non-critical section