java 多线程 赛马

java 多线程 赛马,第1张

package test

import java.util.ArrayList

import java.util.Arrays

import java.util.Collections

import java.util.Comparator

import java.util.HashMap

import java.util.List

import java.util.Map

import java.util.Set

import java.util.SortedMap

import java.util.TreeMap

import sun.util.logging.resources.logging

/**

* 2010-1-17 下午03:08:52 Piaolj

*/

public class Racing implements Runnable {

String str

static Map horses = new TreeMap()/磨态/ 定义一个全局的Map存放5个马的名字和时间

static int count = 0// 用于判断县城是否全部结束

static boolean flag = true// 后面用while(flag)判断count是否为0,如果有兴趣,可以起另外一个线程完成此任务

public Racing(String string) {

// TODO Auto-generated constructor stub

this.str = string

}

public static void main(String[] args) {

Racing ra1 = new Racing("No.1 Horse")

Racing ra2 = new Racing("No.2 Horse")

Racing ra3 = new Racing("No.3 Horse")

Racing ra4 = new Racing("No.4 Horse")

Racing ra5 = new Racing("No.5 Horse")

Racing checkingThread = new Racing("checkingThread")

Thread t1 = new Thread(ra1)

Thread t2 = new Thread(ra2)

Thread t3 = new Thread(ra3)

Thread t4 = new Thread(ra4)

Thread t5 = new Thread(ra5)

t1.start()

t2.start()

t3.start()

t4.start()

t5.start()

while (flag) {

if (count == 0)// 所有线程结束

{

flag = false

}

}

// 排序

List infoIds = new ArrayList(horses.entrySet())

Collections.sort(infoIds, new Comparator<Map.Entry>() {

public int compare(Map.Entry o1, Map.Entry o2) {// 定义了比较的规则,因为这腔盯里是按map的value排序的

Long tmp = Long.parseLong(o1.getValue().toString())

- Long.parseLong(o2.getValue().toString())

return tmp.intValue()

}

})

System.out.println("伍游和输出马的名次:")

System.out.println()

for (int i = 0i <infoIds.size()i++) {

String id = infoIds.get(i).toString()

System.out.println(id)

}

}

public void run() {

// TODO Auto-generated method stub

int CircuitLength = 1000

int breakpoint = 200

int tmpint = 200

long Withtime

count = count + 1// 执行了一个线程,正在执行的线程数加1

// System.out.println(Thread.currentThread().getId())

for (int i = 0i <CircuitLength + 1i++) {

long start = System.currentTimeMillis()

if (i == breakpoint) {

int sleeping = (int) (Math.random() * 5000)

try {

Thread.sleep(sleeping)

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace()

}

breakpoint = breakpoint + tmpint

}

if (i == CircuitLength) {

System.out.print(str + "\t" + "\t")

long end = System.currentTimeMillis()

Withtime = (end - start)

System.out.println("With time is:\t" + Withtime)

// 当每匹马跑完将马的时间的马的名称放入map中

horses.put(str, Withtime)

}

}

count = count - 1// 执行完了一个线程,线程数减1

}

}

时间比较少,写的比较草,呵呵 加了点注释,要是有问题可以发短消息

输出结果:

No.4 Horse With time is: 888

No.3 Horse With time is: 3042

No.5 Horse With time is: 1921

No.2 Horse With time is: 4346

No.1 Horse With time is: 2831

输出马的名次:

No.4 Horse=888

No.5 Horse=1921

No.1 Horse=2831

No.3 Horse=3042

No.2 Horse=4346

在开始比赛前的1h,检察员需要称量骑手的体重,谓之“称磅”,如果体重比标准超过0。4kg(有的国家是1磅,即0。454kg),那么将不允陪槐许参加比赛,如果其体重低于标准,需要附加铅块。在测验的程序册上如果有什么变动,如马匹退出比赛或骑手改变,需要在开始之前30min和比赛进行当中通知赛马场的观众姿培。

在第一场比赛开始前的30min,裁判给予信号,表明比赛正式开幕。第二次给信号是在开始前的15min。在这时,参加比赛的马匹都要进入赛前遛迹乱唯马场亮相。按照裁判的信号,参赛的骑手都骑马进入跑道,给马做准备活动。马鞍的左侧和右侧都应该有号码,同程序册中的号码一样。骑手参加比赛都穿着规定样式和颜色的服装,戴着保护头盔。在比赛前的2~3min给第三次信号,骑手马上进入标有自己号码的起点上,并按起跑员的命令开始比赛。所有不参加比赛的马匹在听到第三次信号时全部应离开跑道。在比赛时,在跑道上遛马是禁止的。

根据流程图,我写了者锋一个完整的程序给你参考。

源代码如下(vc++6.0下编游兄译通过):

#include <stdio.h>

int main(void)

{

int x=0,max=0,min=0,i=0,s=0

printf("please input a number(x): \神嫌袭n")

scanf("%d", &x)

max = x

min = x


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12529590.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存