『壹』 課程設計題目,多線程編程:醫院門診模擬,想用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"
效果如下圖: