操作系统多线程同步与互斥课程设计报告(2)

2018-12-21 13:09

beginActionPerformed():处理开始按钮的方法 endActionPerformed():处理结束按钮的方法

setokActionPerformed():处理参数设置开始按钮的方法 timeActionPerformed():处理暂停按钮的方法 cotinueActionPerformed():处理继续按钮的方法 datasaveActionPerformed():处理保存数据按钮的方法 (3)主要构件

JTextArea:see1、see2、see3,用于显示模拟3个buffer的put,move,get动作; JButton:begin、end 、setok 、time 、cotinue、datasave;

JTextField: empty1、empty2、empty3、full1、full2、full3,实时显示各个buffer已用或剩余空间;

JTextField:room1、room2、room3,ms2、ms3,gs2、gs3,用于设置各个参数。

4.1.2BUFFER类

(1)成员变量或对象:

private final char []buffer1 = new char[Main_win.r1]栈1的空间由设定的参数决定 private final char []buffer2 = new char[Main_win.r2]栈2的空间由设定的参数决定 private final char []buffer3 = new char[Main_win.r3]栈3的空间由设定的参数决定 public int index1 = 0,index2=0,index3=0;

用作角标或指针

private int EmptyFirst,EmptySecond,EmptyThird剩余空间 private static int fullFirst,fullSecond,fullThird 当前字符数 private static int putnum1=0,putnum2=0,putnum3=0共放入数量 private static int getnum1=0,getnum2=0,getnum3=0 共取出数量 static FileWriter data

static BufferedWriter datawriter

static String[] da=new String[1000] 数据保存所用变量、对象 static String[] x=new String[Main_win.r1] static String[] y=new String[Main_win.r2] static String[] z=new String[Main_win.r3]

由于buffer i[]为char,不能添加到JTextField、JTextArea,而String类型能,所以设置String[]类型数组,经转化使char转化为String型,以将字符添加到各个JTextField、JTextArea。

(2)成员方法

具体实现各同步互斥动作的synchronized成员方法:put()、move1()、move2()、get2()、get3();

模拟过程显示到主界面的成员方法:showPush1()、showPush2()、showPush3()、showPop1()、showPop2()、showPop3();

显示数据到统计结果界面的resulewin()方法; 保存数据的datasave()方法。

4.1.3 Runnable接口类

Runnable接口类有五个,分别为:BUFFER_PUT、BUFFERT_MOVE、BUFFERTH_MOVE、BUFFERT_GET和BUFFERTH_GET类。

每个类中成员对象有

private BUFFER buff 用于调用BUFFER类中的synchronized成员方法,以及使用BUFFER 类中共享的变量,实现进程同步与互斥。

4.1.4 Resulr_win类

主要构件:JTextArea resultarea显示最终统计结果。

4.2进程操作所用原语

4.2.1

sleep() :导致当前的线程等待,直到其他线程调用此对象的 notify方法或 notifyAll 方法唤醒。

4.2.2

wait():在指定的毫秒数内让当前正在执行的线程休眠(暂停执行)。该线程不丢失任何监视器的所属权

4.2.3

notify():选择一个wait状态线程进行通知,并使它获得该对象上的锁。

4.3 BUFFER模块内部流程

Put线程 run()

Move线程

run() Get线程 run() BUFFER.put() showPush1()

BUFFER.move() BUFFER.get() put() showPush2、3() showPop1() showPop2、3() Buffer2或3减少一个字符

产生字符

字符显示在主界面 Buffer1中一个字符消失,Buffer2或3增加一个字符 字符显示在主界面

字符显示在主界面 图5 BUFFER内部流程

5. 核心代码

5.1 BUFFER类

由于move(),get()各有两组,此处只给出一组的代码;showPush(),showPop()函数共三组,三组类似,因此这里也只给出一组。 package javaapplication7; import java.awt.*;

import java.awt.event.*;

import static javaapplication7.Result_win.resultarea; import javax.swing.*; import java.io.*; /** *

* @autho * r a101269 */

class BUFFER{

private final char []buffer1 = new char[Main_win.r1];//栈的空间由设定的参数决定 private final char []buffer2 = new char[Main_win.r2];//栈的空间由设定的参数决定 private final char []buffer3 = new char[Main_win.r3];//栈的空间由设定的参数决定 static String[] x=new String[Main_win.r1]; static String[] y=new String[Main_win.r2]; static String[] z=new String[Main_win.r3]; public int index1 = 0,index2=0,index3=0;

private int EmptyFirst,EmptySecond,EmptyThird;//剩余空间 private static int fullFirst,fullSecond,fullThird;//当前字符数

private static int putnum1=0,putnum2=0,putnum3=0; // 共放入数量 private static int getnum1=0,getnum2=0,getnum3=0; // 共取出数量 static FileWriter data;//数据保存 static BufferedWriter datawriter;

public synchronized void put(){ char c='a';//对c初始化

boolean suspended=Main_win.s;//suspended为TRUE则等待,为FALSE执行 if (suspended == true){ try {this.wait();}

catch (InterruptedException e) {} } if(index1 >= buffer1.length)//堆栈已满,不能压栈 {

try{this.wait();}//等待,直到有数据出栈 catch(InterruptedException e){} }

if(index1>=0&&index1

EmptyFirst=buffer1.length-fullFirst;

showPush1(c,index1);//调用界面显示入栈程序

Main_win.full1.setText(String.valueOf(fullFirst));//int 转String Main_win.empty1.setText(String.valueOf(EmptyFirst)); history.his.append(\进字符:\ index1++;//指针向上移动 putnum1++;

} }

public synchronized void move2() {

boolean suspended=Main_win.s; if (suspended == true){ try {this.wait();}

catch (InterruptedException e) {} } if(index1<=0||index2>=buffer2.length-1)//堆栈无数据或满,不能出栈 { try {this.wait();}//等待其他线程把数据入栈 catch(InterruptedException e){} }

if(index1>0&&index1<=buffer1.length&&index2>=0&&index2

this.notify();//通知其他线程入栈 index1--;

char c = buffer1[index1];

buffer2[index2] = c;//数据入栈2

y[index2]=String.valueOf(c); fullSecond=index2+1;

EmptySecond=buffer2.length-fullSecond; fullFirst-=1;

EmptyFirst=buffer1.length-fullFirst;


操作系统多线程同步与互斥课程设计报告(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:关于承担行政职能事业单位改革的几点思考

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: