JAVA龟兔赛跑问题

JAVA龟兔赛跑问题,第1张

先让兔子进入阻塞状态,然后等乌龟跑完终点后唤醒兔子线程就行;下面备顷是各个方法的配套使用(网上copy的,总结的很不错)1. sleep() 方法:sleep() 允许 指定以毫秒为单位的一段时间作为参数,它使得线程在指定的时间

内进入阻塞状态,不能得到CPU 时间,指定的时间一过,线程重新进入可执行状态。

典型地,sleep() 被用在等待某个资源就绪的情形:测试发现条件不满足后,让线程阻塞一段时间后

重新测试,直到条件满足为止。

2. suspend() 和 resume() 方法:两个方法配套使用,suspend()使得线程进入阻塞状态,并且不会

自动恢复,必须银滚瞎其对应的resume() 被调用,才能使得线程重新进入可执行状态。典型地,suspend() 和

resume() 被用在等待另一个线程产生的结果的情形:测试发现结果还没有产生后,让线程阻塞,另一个

线程产生了结果后锋空,调用 resume() 使其恢复。

3. yield() 方法:yield() 使得线程放弃当前分得的 CPU 时间,但是不使线程阻塞,即线程仍处于

可执行状态,随时可能再次分得 CPU 时间。调用 yield() 的效果等价于调度程序认为该线程已执行了足

够的时间从而转到另一个线程。

4. wait() 和 notify() 方法:两个方法配套使用,wait() 使得线程进入阻塞状态,它有两种形式

,一种允许指定以毫秒为单位的一段时间作为参数,另一种没有参数,前者当对应的 notify() 被调用或

者超出指定时间时线程重新进入可执行状态,后者则必须对应的 notify() 被调用。

详情请见http://blog.csdn.net/small____fish/article/details/7726468

用了3个类:写游唯的比较简单,可以参考下

public class Running {

public static void main(String[] args) {

Tuzi tuzi = new Tuzi()

Wugui wugui = new Wugui()

tuzi.begin()

wugui.begin()

}

}

public class Wugui {

private Thread thread

public void begin() {

thread = new Thread(new Runnable() {

public void run() {

while (true) {

move()

try {

Thread.sleep(1000)

} catch (InterruptedException e) {

e.printStackTrace()

}

}

}

})

thread.start()

}

private void move() {

System.out.println("乌龟跑核弊了神氏培5米")

}

}

public class Tuzi {

private Thread thread

public void begin() {

thread = new Thread(new Runnable() {

public void run() {

while (true) {

move()

try {

Thread.sleep(1000)

} catch (InterruptedException e) {

e.printStackTrace()

}

try {

Thread.sleep(5000)

} catch (InterruptedException e) {

e.printStackTrace()

}

System.out.println("兔子醒了")

}

}

})

thread.start()

}

private void move() {

System.out.println("兔子跑了50米,开始睡觉")

}

}

类声瞎游汪明文件

#ifndef THJ_h

#define THJ_h

using namespace std

class tor{

public:

int move() //乌龟移动

}

class har{

public:

int move() //兔子移动

}

class jud{

private:

int t_pos //乌龟位置

int h_pos //兔子位置

enum {race_end=100} //赛道长度

public:

jud(int a=0,int b=0){t_pos=ah_pos=b}

void restart() //重新比赛

void update(int t,int h) //磨扰更新乌龟和磨仔兔子的位置

void print_title() //输出标题

void print() //输出当前乌龟和兔子的位置

bool judge(int &winner)const //判断比赛是否结束及胜者

}

#endif

类实现文件

#include "THJ.h"

#include <iostream>

//#include <ctime>

using namespace std

int tor::move()

{

//srand(time(NULL))

int x=rand()*10/(RAND_MAX+1)

if(x>=0 &&x<=4)

return 3

else if(x>=5 &&x<=6)

return -6

else

return 1

}

int har::move()

{

//srand(time(NULL))

int y=rand()*10/(RAND_MAX+1)

if(y>=0 &&y<=1)

return 0

else if(y>=2 &&y<=3)

return -9

else if(y==4)

return 14

else if(y>=5 &&y<=7)

return 3

else

return -2

}

void jud::restart()

{

t_pos=0

h_pos=0

}

void jud::update(int t,int h)

{

t_pos+=t

h_pos+=h

}

void jud::print_title()

{

cout<<"\t乌龟\t兔子"<<endl

cout<<"\t=======\t======="<<endl

}

void jud::print()

{

cout<<"\t"<<t_pos<<"米\t"<<h_pos<<"米\n"

}

bool jud::judge(int &winner)const

{

if(t_pos>=race_end || h_pos>=race_end){

if(t_pos>h_pos)

winner=0

else if(t_pos<h_pos)

winner=1

else if(t_pos==h_pos)

winner=2

return true

}

else

return false

}

主程序

#include "THJ.h"

#include <iostream>

#include <ctime>

using namespace std

int main0() //龟兔赛跑模拟

{

srand(time(NULL))

tor tor

har har

jud jud

int winner

jud.print_title()

while(!jud.judge(winner)){

jud.update(tor.move(),har.move())

jud.print()

}

switch(winner){

case 0:cout<<"**********乌龟赢啦!**********\n\n"break

case 1:cout<<"**********兔子赢啦!**********\n\n"break

case 2:cout<<"**********平局!**********\n\n"break

}

return 0

}


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存