java 进程同步代码_java 实现进程间的同步(源代码)

本文介绍了一个使用Java实现的进程间同步案例。通过信号量和线程控制,实现了多个进程之间的有序执行与资源竞争的避免。具体包括信号量的初始化、进程状态的控制以及线程的调度策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

操作系统课程设计:实现进程间的同步。 有问题留言

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(); } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值