⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 expthread4.java

📁 java关于多线程的课堂练习
💻 JAVA
字号:
/**
 * @(#)ExpThread4.java
 *
 *
 * @author 
 * @version 1.00 2007/12/1
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Customer extends Thread {
 private int id=-1;
 boolean running=true;
 Win win=null;
 
 public Customer(int id,Win w) {
  this.id=id;
  win=w;
 }
 public void run() {  
  while (running) {  
  int into=(int) (8*Math.random());
  int amount=(int) (1000*Math.random());
  win.transfer(id,into,amount); 
  //yield();
  }  
 }
 public void halt() {
  running=false;
 }
 public void start() {
  running=true;
  super.start();
 }
 
}
class Win extends JFrame implements ActionListener {
 JButton  show=new JButton("Show Accounts");
 JButton  restart=new JButton("Restart");
 JButton  stop=new JButton("Stop");
 JLabel  transferTimes=new JLabel("Transfer Completed: 0");
 JTextArea  displayAccounts=new JTextArea();
 JPanel p=new JPanel();
 Customer  customers[]=new Customer[8];
 int accounts[]=new int[8];
 int transferCount=0;
 Win() {
  p.add(show);
  show.addActionListener(this);
  p.add(restart);
  restart.addActionListener(this);
  p.add(stop);
  stop.addActionListener(this);
  add(p,BorderLayout.SOUTH);
  add(new JScrollPane(displayAccounts),BorderLayout.CENTER);
  add(transferTimes,BorderLayout.NORTH);
  for (int i=0; i<8; i++) {
   accounts[i]=100000;
  }
  for (int i=0; i<8; i++) 
  	customers[i]=new Customer(i,this); 
  for (int i=0; i<8; i++) 	
  	customers[i].start();
  
  
  setSize(400,600);
  setVisible(true);
  validate();
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
 
 public  /*synchronized*/  void transfer(int from,int into,int amount) {
  if (accounts[from]>=amount && from!=into) {
  	transferCount++;
  	transferTimes.setText("Transfers completed: "+transferCount);
  	//System.out.println("当前是"+from+"在转帐 转给"+into+"金额为"+amount+"第"+transferCount+"次转帐\n");
  	//validate();
  	int a=accounts[from]-amount;
  	int b=accounts[into]+amount;
  	try {
  	// System.out.println("线程"+from+"开始休息...\n");
  		Thread.sleep(100);
  	 //System.out.println("线程"+from+"休息完毕!\n");
  	}
  	catch (InterruptedException ie) {
  	 System.err.println("interruption in sleeping..."+ie);
  	}
  	//System.out.println("线程"+from+"设置金额\n");
  	accounts[from]=a;
  	accounts[into]=b;
  	
  }
 }
 public void actionPerformed(ActionEvent e) {
 	if (e.getSource()==restart) { 
 	 transferCount=0;
 	 transferTimes.setText("Transfers completed: 0");
 	 for (int i=0; i<8; i++) 
 	   customers[i].halt();
 	 for (int i=0; i<8; i++)
 	   customers[i]=new Customer(i,this);
 	 for (int i=0; i<8; i++)
 	   customers[i].start();	   
 	
 	}
 	else if (e.getSource()==stop) {
 	 for (int i=0; i<8; i++)
 	   customers[i].halt();
 	}
 	else if (e.getSource()==show) {
 	 int sum=0;
 	 displayAccounts.setText("");
 	 for (int i=0; i<8; i++) {
 	   displayAccounts.append("Account "+i+":$"+accounts[i]+"\n");
 	   sum+=accounts[i];
 	 }
 	 displayAccounts.append("Total Amount: &"+sum+"\n");
 	 displayAccounts.append("Total Transfers: "+transferCount);
 	}
 }
}
public class ExpThread4 {
        
    /**
     * Creates a new instance of <code>ExpThread4</code>.
     */
    public ExpThread4() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
       new Win();        
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -