上机实验-9 多线程案例

本文介绍了使用Java实现的多线程实验,包括抽奖系统(两个抽奖箱随机抽取奖项)、电影院售票(继承Thread和实现Runnable接口),以及模拟月亮围绕地球的动画示例。
摘要由CSDN通过智能技术生成

一、实验目的

1、了解多线程程序的设计思路。

2、掌握多线程程序的编写和线程同步的解决。

3、学习使用Timer类创建线程。

二、实验内容 

1、有一个抽奖池,该抽奖池中存放了奖励的金额,该抽奖池用一个数组int[] arr = {10,5,20,50,100,200,500,800,2,80,300};创建两个抽奖箱(线程)设置线程名称分别为抽奖箱1抽奖箱2,随机从arr数组中获取奖项元素并打印在控制台上

ChoujiangDemo

public class ChoujiangDemo {
public static void main(String[] args) {
   		Chou c = new Chou();
   		Thread t1=new Thread(c,"抽奖盒1");//创建线程对象t1,命名为“抽奖箱1”
   		Thread t2=new Thread(c,"抽奖盒2");//创建线程对象t2,命名为“抽奖箱2”
  			 t1.start();
  			 t2.start();
}
}

Chou

public class Chou implements Runnable {
    int[] arr = { 10, 5, 20, 50, 100, 200, 500, 800, 2, 80, 300 };
    int num = arr.length;
    boolean[] flag = new boolean[arr.length];

    public void run() {

        while (true) {
            synchronized (this) {

                if (num > 0) {
                    int index = (int)(Math.random()*arr.length);
                    int get = arr[index];
                    if (flag[index] != true) {

                        flag[index] = true;
                        System.out.println(Thread.currentThread().getName()
                                + "又产生了一个" + get + "元大奖");
                        num--;
                    }
                }
            }
        }
    }
}

2、某电影院目前正在上映贺岁大片,共有100张票,而它有3个售票窗口售票,请设计一个程序模拟该电影院售票。两种方式实现:继承Thread类、实现Runnable接口。

(1)继承 Thread 类

BuyTicketT

public class BuyTicketT { 
public static void main(String[] args) {//主函数 
SaleWindowThread1 w1 = new SaleWindowThread1(); 
Thread t1 = new Thread(w1, "第一个售票窗口");//模拟三个售票窗口 
Thread t2 = new Thread(w1, "第二个售票窗口"); 
Thread t3 = new Thread(w1, "第三个售票窗口"); 
t1.start(); 
//启动第一个售票窗口,开始售票 
t2.start(); 
//启动第二个售票窗口,开始售票 
t3.start(); 
//启动第三个售票窗口,开始售票 
} 
}

SaleWindowThread1

class SaleWindowThread1 extends Thread { 
//实现 Runnable 接口 
private static int total_count = 100; 
//电影票总数 
@Override 
public void run() { //重写 run()方法 
while (true) { 
if (total_count > 0) { 
synchronized(this){ 
if (total_count > 0){ 
System.out.println(Thread.currentThread().getName() 
+ "售出第" + (100 - total_count + 1) + "张票。还剩"+ (total_count - 1) + "张"); 
//记录售出票和剩余票数 
total_count--; 
} 
} 
} 
else { 
break; 
} 
} 
} 
}

(2)实现 Runnable 接口

BuyTicketR

public class BuyTicketR { 
public static void main(String[] args) {//主函数 
SaleWindowThread w1 = new SaleWindowThread(); 
Thread t1 = new Thread(w1, "第一个售票窗口");//模拟三个售票窗口 
Thread t2 = new Thread(w1, "第二个售票窗口"); 
Thread t3 = new Thread(w1, "第三个售票窗口"); 
t1.start(); 
//启动第一个售票窗口,开始售票 
t2.start(); 
//启动第二个售票窗口,开始售票 
t3.start(); 
//启动第三个售票窗口,开始售票 
} 
} 

SaleWindowThread

class SaleWindowThread implements Runnable { 
//实现 Runnable 接口 
private static int total_count = 100; 
//电影票总数 
@Override 
public void run() { //重写 run()方法 
while (true) { 
if (total_count > 0) { 
synchronized(this){ 
if (total_count > 0){ 
System.out.println(Thread.currentThread().getName() 
+ "售出第" + (100 - total_count + 1) + "张票。还剩"+ (total_count - 1) + "张"); 
//记录售出票和剩余票数 
total_count--; 
} 
} 
} 
else { 
break; 
} 
} 
} 
} 

3、编写一个应用程序,模拟月亮围绕地球。

MainClass

import javax.swing.*; 
public class MainClass { 
public static void main(String args[]) { 
Sky sky= new Sky(); 
JFrame frame = new JFrame(); 
frame.add(sky); 
frame.setSize(500,500); 
frame.setVisible(true); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
frame.getContentPane().setBackground(java.awt.Color.white); 
}}

Earth

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
public class Earth extends JLabel implements ActionListener { 
private static final long serialVersionUID = 1L; 
JLabel moon; //显示月亮之外观 
Timer timer; 
double pointX[]=new double[360], 
pointY[]=new double[360]; 
int w=200,h=200,i=0; 
Earth() { 
setLayout(new FlowLayout()); 
setPreferredSize(new Dimension(w,h));//使用了布局管理器,则使用该方法设置组件的尺寸。 
timer=new Timer(20,this);//创建 timer,振铃间隔是 20 毫秒当前 Earth 对象为其监视器 
setIcon(new ImageIcon("src/images/earth.jpg")); 
setHorizontalAlignment(SwingConstants.CENTER); 
moon=new JLabel(new 
ImageIcon("src/images/moon.jpg"),SwingConstants.CENTER); 
add(moon); 
moon.setPreferredSize(new Dimension(60,60)); 
pointX[0]=0; 
pointY[0]=h/2; 
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]=0.8*pointX[i]+w/2; //坐标缩放、平移 
pointY[i]=0.8*pointY[i]+h/2; 
} 
timer.start(); 
} 
public void actionPerformed(ActionEvent e) { 
i=(i+1)%360; 
moon.setLocation((int)pointX[i]-30,(int)pointY[i]-30); 
} 
} 

Sky

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
public class Sky extends JLabel implements ActionListener { 
private static final long serialVersionUID = 1L; 
Earth earth; 
Timer timer; 
double pointX[]=new double[360], 
pointY[]=new double[360]; 
int w=400,h=400,i=0; 
Sky() { 
setLayout(new FlowLayout()); 
timer= new Timer(100,this);// 创建 timer,振铃间隔是 100 毫秒当前 Sky 对象为其监视器 
setPreferredSize(new Dimension(w,h)); 
earth = new Earth(); 
add(earth); 
earth.setPreferredSize(new Dimension(200,200)); 
pointX[0]=0; 
pointY[0]=h/2; 
double angle=1*Math.PI/180; //此时的 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]=0.5*pointX[i]+w/2; //坐标缩放、平移 
pointY[i]=0.5*pointY[i]+h/2; 
} 
timer.start(); 
} 
public void actionPerformed(ActionEvent e) { 
i=(i+1)%360; 
earth.setLocation((int)pointX[i]-100,(int)pointY[i]-100); 
} 
} 

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值