您的位置:首页 > 编程语言 > Java开发

java 实验四 多线程(无脑实验系列)

2016-05-23 09:12 441 查看
1.汉字打字练习。

² 实验要求:

编写一个Java应用程序,在主线程中再创建一个Frame类型的窗口,在该窗口中再创建1个线程giveWord。线程giveWord每隔2秒钟给出一个汉字,用户使用一种汉字输入法将该汉字输入到文本框中。

package lzy;
public class ThreadWordMainClass
{  public static void main(String args[])
{
new ThreadFrame();
}
}


package lzy;

import java.awt.*;
import java.awt.event.*;
public class ThreadFrame extends Frame implements ActionListener
{
Label  wordLabel;
Button button;
TextField inputText,scoreText;
WordThread  giveWord;//用WordThread声明一个giveWord对象
int score=0;
ThreadFrame()
{ wordLabel=new Label(" ",Label.CENTER);
wordLabel.setFont(new Font("",Font.BOLD,72));
button=new Button("开始");
inputText=new TextField(3);
scoreText=new TextField(5);
scoreText.setEditable(false);
giveWord=new WordThread(wordLabel);//创建giveWord,将wordLabel传递给WordThread构造方法的参数
button.addActionListener(this);
inputText.addActionListener(this);
add(button,BorderLayout.NORTH);
add(wordLabel,BorderLayout.CENTER);
Panel southP=new Panel();
southP.add(new Label("输入标签所显示的汉字后回车:"));
southP.add(inputText);
southP.add(scoreText);
add(southP,BorderLayout.SOUTH);
setBounds(100,100,350,180);
setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
}
);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button)
{  if(!(giveWord.isAlive()))     //giveWord调用方法isAlive()
{    giveWord=new WordThread(wordLabel);
}
try
{    giveWord.start();//giveWord调用方法start()
}
catch(Exception exe){}
}
else if(e.getSource()==inputText)
{  if(inputText.getText().equals(wordLabel.getText()))
{  score++;
}
scoreText.setText("得分:"+score);
inputText.setText(null);
}
}
}


package lzy;

import java.awt.*;
public class WordThread extends Thread
{   char word;
int k=19968;
Label com;
WordThread(Label com)
{  this.com=com;
}
public void run()
{  k=19968;
while(true)
{
word=(char)k;
com.setText(""+word);
try{
sleep(6000);//调用sleep方法使得线程中断6000豪秒
}
catch(InterruptedException e){}
k++;
if(k>=29968) k=19968;
}
}
}


2.旋转的行星。

² 实验要求:

编写一个应用程序,模拟月亮围绕地球旋转、地球围绕太阳旋转。

package lzy;

public class ThreadRotateMainClass
{  public static void main(String args[])
{
new HaveThreadFrame();
}
}


package lzy;

import java.awt.*;
public class Mycanvas extends Canvas
{
int r;
Color c;
public void setColor(Color c)
{
this.c=c;
}
public void setR(int r)
{
this.r=r;
}
public void paint(Graphics g)
{
g.setColor(c);
g.fillOval(0,0,2*r,2*r);
}
public int getR()
{
return r;
}
}


package lzy;

import java.awt.*;
import java.awt.event.*;
public class HaveThreadFrame extends Frame implements Runnable
{
Thread rotate;     //用Thread类声明一个rotate对象
Planet earth;
double pointX[]=new double[360],
pointY[]=new double[360];
int width,height;
int radius=120;
HaveThreadFrame()
{
rotate=new Thread(this);
earth=new Planet();
setBounds(0,0,360,400);
width=getBounds().width;
height=getBounds().height;
pointX[0]=0;
pointY[0]=-radius;
double angle=1*Math.PI/180;
for(int i=0;i<359;i++)
{
pointX[i+1]=pointX[i]*Math.cos(angle)-Math.sin(angle)*pointY[i];
pointY[i+1]=pointY[i]*Math.cos(angle)+pointX[i]*Math.sin(angle);
}
for(int i=0;i<360;i++)
{
pointX[i]=pointX[i]+width/2;
pointY[i]=pointY[i]+height/2;
}
setLayout(null);
setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
add(earth);
earth.setLocation((int)pointX[0]-earth.getSize().width/2,
(int)pointY[0]-earth.getSize().height/2);
earth.start();
rotate.start();   //用rotate调用start方法
}
public void run()
{ int i=0;
while(true)
{
i=(i+1)%360;
earth.setLocation((int)pointX[i]-earth.getSize().width/2,
(int)pointY[i]-earth.getSize().height/2);
try{
Thread.sleep(100);
}
catch(InterruptedException e){}
}
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillOval(width/2-15,height/2-15,30,30);
}
}


package lzy;

import java.awt.*;
public class Planet extends Panel implements Runnable
{
Thread moon;     //用Thread类声明一个moon对象
Mycanvas yellowBall;
double pointX[]=new double[360],
pointY[]=new double[360]; //用来表示画布左上角端点坐标的数组
int w=100,h=100;
int radius=30;
Planet()
{
setSize(w,h);
setLayout(null);
yellowBall=new Mycanvas();
yellowBall.setColor(Color.yellow);
add(yellowBall);
yellowBall.setSize(12,12);
yellowBall.setR(12/2);
pointX[0]=0;
pointY[0]=-radius;
double angle=1*Math.PI/180;   //刻度为1度
for(int i=0;i<359;i++)        //计算出数组中各个元素的值
{
pointX[i+1]=pointX[i]*Math.cos(angle)-Math.sin(angle)*pointY[i];
pointY[i+1]=pointY[i]*Math.cos(angle)+pointX[i]*Math.sin(angle);
}
for(int i=0;i<360;i++)
{
pointX[i]=pointX[i]+w/2;    //坐标平移
pointY[i]=pointY[i]+h/2;
}
yellowBall.setLocation((int)pointX[0]-yellowBall.getR(),(int)pointY[0]-yellowBall.getR());
moon =new Thread(this); //创建 moon线程,当前面板做为该线程的目标对象
}
public void start()
{
try{
moon .start();
}
catch(Exception exe){ }
}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.fillOval(w/2-9,h/2-9,18,18);
}
public void run()
{
int i=0;
while(true)
{
i=(i+1)%360;
yellowBall.setLocation((int)pointX[i]-yellowBall.getR(),
(int)pointY[i]-yellowBall.getR());
try{
Thread.sleep(10); // Thread类调用类方法sleep使得线程中断10豪秒
}
catch(InterruptedException e){}
}
}
}


3.双线程接力。

² 实验要求:

编写一个应用程序,除了主线程外,还有两个线程:first和second。first负责模拟一个红色的按钮从坐标(10,60)运动到(100,60);second负责模拟一个绿色的按钮从坐标(100,60)运动到(200,60)。

MoveButtonMainClass.java

package lzy;

public class MoveButtonMainClass
{
public static void main(String args[])
{
new MoveButton();
}
}


MoveButton.java

package lzy;
import java.awt.*;
import java.awt.event.*;
public class MoveButton extends Frame implements Runnable,ActionListener
{
Thread first,second;//用Thread类声明first,second两个线程对象
Button redButton,greenButton,startButton;
int distance=10;
MoveButton()
{
first=new Thread(this); //创建first线程,当前窗口做为该线程的目标对象
second=new Thread(this); //创建first线程,当前窗口做为该线程的目标对象
redButton=new Button();
greenButton=new Button();
redButton.setBackground(Color.red);
greenButton.setBackground(Color.green);
startButton=new Button("start");
startButton.addActionListener(this);
setLayout(null);
add(redButton);
redButton.setBounds(10,60,15,15);
add(greenButton);
greenButton.setBounds(100,60,15,15);
add(startButton);
startButton.setBounds(10,100,30,30);
setBounds(0,0,300,200);
setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public void actionPerformed(ActionEvent e)
{
try{
first.start();
second.start();
}
catch(Exception exp){}
}
public void run()
{  while(true)
{  if(Thread.currentThread()==first) //判断当前占有CPU资源的线程是否是first
{   moveComponent(redButton);
try{ Thread.sleep(20);
}
catch(Exception exp){}
}
if(Thread.currentThread()==second) //判断当前占有CPU资源的线程是否是second
{   moveComponent(greenButton);
try{ Thread.sleep(10);
}
catch(Exception exp){}
}
}
}
public synchronized void moveComponent(Component b)
{
if(Thread.currentThread()==first)
{   while(distance>100&&distance<=200)
try{ wait();
}
catch(Exception exp){}
distance=distance+1;
b.setLocation(distance,60);
if(distance>=100)
{  b.setLocation(10,60);
notifyAll();
}
}
if(Thread.currentThread()==second)
{
while(distance>=10&&distance<100)
try{ wait();
}
catch(Exception exp){}
distance=distance+1;
b.setLocation(distance,60);
if(distance>200)
{  distance=10;
b.setLocation(100,60);
notifyAll();
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: