java 實現進程間的同步(原始碼)

來源:互聯網
上載者:User

作業系統課程設計:實現進程間的同步。 有問題留言


import java.util.concurrent.Semaphore; /******** 訊號量 *******/
class Sign {
final static int NUMS = 6; // 進程的數目
final static int RUN = 1; // 運行完
final static int STOP = 0; // 阻塞態
static int[] status = new int[NUMS]; // 進程的狀態 不運行 則阻塞 預設為禁止
static Semaphore s[] = null; // 訊號量
final static int[] times = new int[6]; //啟動並執行次數


static Semaphore mutex = new Semaphore(1);// 訊號量:控制對臨界區的訪問


static {
s = new Semaphore[NUMS];
for (int i = 0; i < NUMS; i++) {
s[i] = new Semaphore(1); // 初始化
}
for (int j = 0; j < times.length; j++) {
times[j] = 10; // 每個進程執行10次則結束
}
}
}


/*********線程***********/
class process6 implements Runnable {// 進程線程
private int id;


public process6(int id) {
super();
this.id = id;
}


void test(int id) throws InterruptedException { // 測試當前進程是否可以運行
// 當自己沒有運行,前置條件滿足時 可以運行
switch (id) {
case 0:


if (Sign.times[0] > 0) {
Sign.s[0].release();
Sign.times[0]--;
System.out.println("I am thread P" + (id + 1) + "。。。。。"
+ Sign.times[id]);
}
;
break;
case 1:


if (Sign.status[0] == Sign.RUN && Sign.times[1] > 0) { // 如果進程1
// 已經運行完,進程2可以直接運行


Sign.s[1].release();
Sign.times[1]--;
System.out.println("I am thread P" + (id + 1) + "。。。。。"
+ Sign.times[id]);
}
;
break;
case 2:


if (Sign.status[0] == Sign.RUN && Sign.times[2] > 0) { // 如果進程1
// 已經開啟,進程3可以直接運行


Sign.s[2].release();


Sign.times[2]--;
System.out.println("I am thread P" + (id + 1) + "。。。。。"
+ Sign.times[id]);
}
;
break;
case 3:


if (Sign.status[1] == Sign.RUN && Sign.times[3] > 0) { // 如果進程2已經開啟,進程4可以直接運行


Sign.s[3].release();
Sign.times[3]--;
System.out.println("I am thread P" + (id + 1) + "。。。。。"
+ Sign.times[id]);
}
;
break;
case 4:


if (Sign.times[4] > 0
&& (Sign.status[1] == Sign.RUN || Sign.status[2] == Sign.RUN)) {// 如果進程2、或3已經運行完,進程5可以直接運行


Sign.s[4].release();
Sign.times[4]--;
System.out.println("I am thread P" + (id + 1) + "。。。。。"
+ Sign.times[id]);
}
;
break;
case 5:


if (Sign.times[5] > 0
&& (Sign.status[4] == Sign.RUN || Sign.status[3] == Sign.RUN)) {// 如果進程4或5已經運行完,進程6可以直接運行


Sign.s[5].release();
Sign.times[5]--;
System.out.println("I am thread P" + (id + 1) + "。。。。。"
+ Sign.times[id]);
}
;
break;
}
}


void print() {// 列印 每個進程的狀態
for (int i = 0; i < Sign.NUMS; i++) {
System.out.println("Thread P" + (i + 1) + " :" + Sign.status[i]);
}
}


@Override
public void run() {
try {
while (true) {
// print(); //先列印全部進程狀態
Sign.mutex.acquire();
// System.out.println("P" + id + " is testing!");
test(id);
Sign.mutex.release();
// 判斷當前進程啟動並執行狀況,不能運行 則 阻塞
Sign.s[id].acquire();


// 喚醒其他進程 不能運行 則阻塞
if (Sign.times[id] == 0 || Sign.times[id] < 0) {
Sign.status[id] = Sign.RUN;
// System.out.println("P" + (id+1) + " run out!");


Sign.mutex.acquire();
test((id + 1) % 6);
test((id + 2) % 6);
test((id + 3) % 6);
test((id + 4) % 6);
test((id + 5) % 6);
Sign.mutex.release();


} else {
// System.out.println(+"前置條件不夠。");
Thread.currentThread().sleep((100));
}
}
} catch (Exception e) {
e.printStackTrace();
}


}


}


public class SYSV {


/**
* 任務3 進程/線程同步
*/
public static void main(String[] args) {
new Thread(new process6(0)).start();
new Thread(new process6(1)).start();
new Thread(new process6(2)).start();
new Thread(new process6(3)).start();
new Thread(new process6(4)).start();
new Thread(new process6(5)).start();
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.