‘壹’ 课程设计题目,多线程编程:医院门诊模拟,想用java实现,求大神指点
典型的生产者消费者模型。
了解j5的并发库,那个并发库中有适合组件实现。
如果不了解,这么来:
创建一个队列,此队列要求线程安全,如果队列为空则消费者阻塞。如果队列达到某个最大值,则阻塞生产者。
队列用,普通的list或实现好的队列包装成线程安全的。
用synchronized同步原方法或代码块。
写一个或n个线程,模拟病人,排队办理业务,往上面的队列中添加数据。
当达到队列的最大容积,阻塞,等待生产者线程取数据。
阻塞:makerLock.wait();//虚拟机会出让线程挂起,其实就是操作系统,保存当前线程在cpu上的运行状态。再出让线程正在使用的cpu资源,占用的内存不会释放。
往队列插入数据的时候,因为不知道是否有消费者处于等待状态,通知消费者:
customerLock.notifyAll();//虚拟机调度消费者线程运行,实际上是操作系统,把保存的消费者线程状态,从新加载到cpu中接着运行。接着运行线程是任意的,取决于不同操作系统的线程调度算法。
消费者线程读取一个数据后,要通知生产者,可以继续,道理同上:
makerLock.notifyAll();
队列中,无数据可读的时候:
customerLock.wait();//原理同上,
最后注意,生产者跟消费者使用了两个不同的对象锁。lock.wait()的使用方法是这样的:
synchronized(lock){
......
while(condition==true){
lock.wait();
}
......
Objecto=queen.pop();
lock.notifyAll();
}
最后启动n个线程读队列,模拟办理业务的窗口;n个线程写队列,模拟病人排队。
新线程库也有跟老线程库对应的方法,新线程库有线程安全的高效队列。没有上面麻烦,但上面写的是理解新线程数据结构与实现的基础。
packagecom.;
importjava.util.LinkedList;
importjava.util.List;
importjava.util.Random;
publicclassTestThread2{
//缓冲上限
privatelongbufsize;
//缓冲
privateList<String>buf;
publicTestThread2(){
bufsize=5;
buf=newLinkedList<String>();
}
//生产者调用
publicvoidput(Strings){
//模拟生产者跟不上消费者
/*
try{
Thread.sleep(100);
}catch(InterruptedExceptione){
}
*/
synchronized(this){
//超过队列限制就等待
while(buf.size()==bufsize){
System.out.println("队列已满,生产者:"+Thread.currentThread().getId()+"开始等待。");
try{
this.wait();
}catch(InterruptedExceptione){
}
}
buf.add(s);
//通知消费者
this.notifyAll();
}
}
//消费者调用
synchronizedpublicStringtake(){
//模拟消费者跟不上生产者
try{
Thread.sleep(100);
}catch(InterruptedExceptione){
}
Strings=null;
synchronized(this){
while(buf.size()==0){
System.out.println("队列为空,消费者:"+Thread.currentThread().getId()+"开始等待。");
try{
this.wait();
}catch(InterruptedExceptione){
}
}
//取先放入的元素,并移除
s=buf.get(0);
buf.remove(0);
//通知生产者
this.notifyAll();
}
returns;
}
publicstaticvoidmain(String[]args){
//自己实现的,安全队列
finalTestThread2tt=newTestThread2();
//生产者
Threadp=newThread(newRunnable(){
@Override
publicvoidrun(){
while(!Thread.currentThread().isInterrupted()){
Randomr=newRandom();
tt.put(String.valueOf(r.nextInt(10)));
}
}
});
//消费者
Threadc1=newThread(newRunnable(){
@Override
publicvoidrun(){
while(!Thread.currentThread().isInterrupted()){
System.out.println("线程:"+Thread.currentThread().getId()+"获取到数据"+tt.take());
}
}
});
Threadc2=newThread(newRunnable(){
@Override
publicvoidrun(){
while(!Thread.currentThread().isInterrupted()){
System.out.println("线程:"+Thread.currentThread().getId()+"获取到数据"+tt.take());
}
}
});
p.start();
c1.start();
c2.start();
try{
p.join();
c1.join();
c2.join();
}catch(InterruptedExceptione){
}
}
}
‘贰’ java 课程设计
//父类MyMedia
public class MyMedia{
protected String mediaName;
protected float price;
protected String press;
protected String artist;
public MyMedia(String mn,float pri,String pre,String art){
mediaName=mn;
price=pri;
press=pre;
artist=art;
}
public void mediaNameInput(String mn){
mediaName=mn;
}
public void mediaPriceInput(float pri){
price=pri;
}
public void mediaPressInput(String pre){
press=pre;
}
public void mediaArtistInput(String art){
artist=art;
}
}
//子类MyBook
public class MyBook extends MyMedia{
private String editor;
private String publishDate;
private String bookISBN;
public MyBook(String mn,float pri,String pre,String art,String edi,String pub,String isbn){
super(mn,pri,pre,art);
editor=edi;
publishDate=pub;
bookISBN=isbn;
}
public void bookOtherInfo(String edi,String pub,String isbn){
editor=edi;
publishDate=pub;
bookISBN=isbn;
}
public void getBookInfo(){
System.out.println();
System.out.println("书名: "+mediaName);
System.out.println("书的价格:"+price);
System.out.println("书的出版社:"+press);
System.out.println("书的作者:"+artist);
System.out.println("书的editor:"+editor);
System.out.println("书的出版日期:"+publishDate);
System.out.println("书的bookISBN:"+bookISBN);
}
}
//子类MyCD
public class MyCD extends MyMedia{
private String cdISRC;
private String cdPublisher;
public MyCD(String mn,float pri,String pre,String art,String isrc,String cdp){
super(mn,pri,pre,art);
cdISRC=isrc;
cdPublisher=cdp;
}
public void CDOtherInfo(String isrc,String cdp){
cdISRC=isrc;
cdPublisher=cdp;
}
public void getCDInfo(){
System.out.println();
System.out.println("CD名: "+mediaName);
System.out.println("CD的价格:"+price);
System.out.println("CD的出版社:"+press);
System.out.println("CD的演唱者:"+artist);
System.out.println("CD的cdISRC:"+cdISRC);
System.out.println("CD的发行者:"+cdPublisher);
}
}
//子类MyTape
public class MyTape extends MyMedia{
private String TapeISRC;
public MyTape(String mn,float pri,String pre,String art,String isrc){
super(mn,pri,pre,art);
TapeISRC=isrc;
}
public void TapeOtherInfo(String isrc){
TapeISRC=isrc;
}
public void getTapeInfo(){
System.out.println();
System.out.println("CD名: "+mediaName);
System.out.println("CD的价格:"+price);
System.out.println("CD的出版社:"+press);
System.out.println("CD的演唱者:"+artist);
System.out.println("CD的TapeISRC:"+TapeISRC);
}
}
未完代叙 时间太晚了!!
QQ 383513327
‘叁’ Java课程设计!急!!!(高分)
我帮你编写了一部分,实现了“输入十个同学的相关信息,并在文本框中显示”(图形界面实现)。
要实现接下去的功能其实也真的不难的,但是真的很麻烦、很浪费时间……我就帮你做到这里了,你自己添加一下代码就可以(或者提高悬赏的话可以考虑考虑啊!哈哈……)代码如下:
importjava.awt.BorderLayout;
importjavax.swing.JPanel;
importjavax.swing.JFrame;
importjava.awt.Dimension;
importjavax.swing.JButton;
importjava.awt.Rectangle;
importjavax.swing.JLabel;
importjavax.swing.SwingConstants;
importjavax.swing.JScrollPane;
importjavax.swing.JTextArea;
importjavax.swing.JOptionPane;
{
=1L;
privateJPaneljContentPane=null;
privateJButtonjButton=null;
privateJLabeljLabel=null;
privateJScrollPanejScrollPane=null;
privateJTextAreajTextArea=null;
/**
*Thisisthedefaultconstructor
*/
publicTongJi(){
super();
initialize();
}
/**
*Thismethodinitializesthis
*
*@returnvoid
*/
privatevoidinitialize(){
this.setSize(412,372);
this.setContentPane(getJContentPane());
this.setTitle("成绩统计");
this.addWindowListener(newjava.awt.event.WindowAdapter(){
publicvoidwindowClosing(java.awt.event.WindowEvente){
System.exit(0);
}
});
this.setVisible(true);
}
/**
*
*
*@returnjavax.swing.JPanel
*/
privateJPanelgetJContentPane(){
if(jContentPane==null){
jLabel=newJLabel();
jLabel.setBounds(newRectangle(18,66,65,18));
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
jLabel.setText("统计结果:");
jContentPane=newJPanel();
jContentPane.setLayout(null);
jContentPane.add(getJButton(),null);
jContentPane.add(jLabel,null);
jContentPane.add(getJScrollPane(),null);
}
returnjContentPane;
}
/**
*ThismethodinitializesjButton
*
*@returnjavax.swing.JButton
*/
privateJButtongetJButton(){
if(jButton==null){
jButton=newJButton();
jButton.setBounds(newRectangle(18,16,86,28));
jButton.setText("开始统计");
jButton.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEvente){
/////录入成绩信息
String[][]mymsg=newString[10][6];
for(inti=0;i<10;i++){
Stringstrnum=JOptionPane.showInputDialog(null,"请输入第"+(i+1)+"个学生的学号","信息录入",JOptionPane.WARNING_MESSAGE);
Stringstrname=JOptionPane.showInputDialog(null,"请输入第"+(i+1)+"个学生的姓名","信息录入",JOptionPane.WARNING_MESSAGE);
Stringdoublemath=JOptionPane.showInputDialog(null,"请输入第"+(i+1)+"个学生的数学成绩","信息录入",JOptionPane.WARNING_MESSAGE);
Stringdoubleeng=JOptionPane.showInputDialog(null,"请输入第"+(i+1)+"个学生的英语成绩","信息录入",JOptionPane.WARNING_MESSAGE);
Stringdoublejava=JOptionPane.showInputDialog(null,"请输入第"+(i+1)+"个学生的JAVA成绩","信息录入",JOptionPane.WARNING_MESSAGE);
Stringdoublecomp=JOptionPane.showInputDialog(null,"请输入第"+(i+1)+"个学生的计算机成绩","信息录入",JOptionPane.WARNING_MESSAGE);
mymsg[i][0]=strnum;
mymsg[i][1]=strname;
mymsg[i][2]=doublemath;
mymsg[i][3]=doubleeng;
mymsg[i][4]=doublejava;
mymsg[i][5]=doublecomp;
}
////显示成绩信息
jTextArea.setText("学号姓名数学英语JAVA计算机");
for(inti=0;i<10;i++){
jTextArea.setText(jTextArea.getText()+" ");
for(intj=0;j<6;j++){
jTextArea.setText(jTextArea.getText()+mymsg[i][j]+"");
}
}
}
});
}
returnjButton;
}
/**
*
*
*@returnjavax.swing.JScrollPane
*/
(){
if(jScrollPane==null){
jScrollPane=newJScrollPane();
jScrollPane.setBounds(newRectangle(18,86,370,230));
jScrollPane.setViewportView(getJTextArea());
}
returnjScrollPane;
}
/**
*
*
*@returnjavax.swing.JTextArea
*/
privateJTextAreagetJTextArea(){
if(jTextArea==null){
jTextArea=newJTextArea();
jTextArea.setEditable(false);
}
returnjTextArea;
}
publicstaticvoidmain(Stringargs[]){
newTongJi();
}
}//@jve:decl-index=0:visual-constraint="10,10"
效果如下图: