gpt4 book ai didi

java - Java 中的线程,状态?另外,正确的使用方法是什么?

转载 作者:行者123 更新时间:2023-12-01 19:09:06 25 4
gpt4 key购买 nike

我要准备后天的考试演示,所以我需要在考试前弄清楚一些事情,希望你们能帮助我。

首先我确实知道线程有 4 种状态(即运行、就绪、阻塞、终止),但是我不太确定它在 Java 中是如何工作的。在我的代码中,我使用 thread.sleep(3000) 在程序中进行一些等待,这是否会使线程阻塞或就绪?

此外,我还注意到我可能没有以正确的方式使用线程,让我向您展示一些代码

public class BattleHandler implements Runnable {

private Player player;
private Monster enemyMonster;
private Dungeon dungeon;
private JTextArea log;
private GameScreen gScreen;


public void run() {
try {
runBattle();
}
catch(Exception e) { System.out.println(e);}
}

public BattleHandler(Player AttackingPlayer, JTextArea log, GameScreen gScreen) {
this.player = AttackingPlayer;
this.log = log;
this.gScreen = gScreen;
}

public void setDungeon(Dungeon dungeon) {
this.dungeon = dungeon;
}

public Dungeon getDungeon() {
return dungeon;
}

public Monster getEnemyMonster() {
return enemyMonster;
}

public void setMonster() {

// First check if dungeon have been init, if not we can't generate the mob
if(dungeon != null) {

enemyMonster = new Monster();
// Generate monster stats
enemyMonster.generateStats(dungeon);

}else {
System.out.println("Dungeon was not initialized");
}

}
public void runBattle() throws InterruptedException {

// Start battle, and run until a contester is dead.

while(player.getHealth() > 0 && enemyMonster.getHealth() > 0) {

int playerStrikeDmg = player.strike();

if(enemyMonster.blockDefend()) {
log.setText( log.getText() + "\n" + player.getName() +" tried to strike " + enemyMonster.getName()+ ", but " + enemyMonster.getName() + " Blocked.");
}else if(enemyMonster.dodgeDefend()) {
log.setText( log.getText() + "\n" + player.getName() +" tried to strike " + enemyMonster.getName()+ ", but " + enemyMonster.getName() + " Blocked.");
}else {
enemyMonster.defend(playerStrikeDmg);
log.setText( log.getText() + "\n" + player.getName() +" strikes " + enemyMonster.getName()+ " for: " + playerStrikeDmg + " left: "+ enemyMonster.getHealth());
}



if(enemyMonster.getHealth() < 1) break;

Thread.sleep(3000);

// Monster Turn

int monsterDmg = enemyMonster.strike();


if(player.blockDefend()) {
log.setText( log.getText() + "\n" + enemyMonster.getName() +" tried to strike " + player.getName()+ ", but " + player.getName()+ " Blocked.");
}else if(player.dodgeDefend()) {
log.setText( log.getText() + "\n" + enemyMonster.getName() +" tried to strike " + player.getName()+ ", but " + player.getName()+ " Dodged.");
}else {
player.defend(monsterDmg);
log.setText( log.getText() + "\n" + enemyMonster.getName() +" strikes " + player.getName()+ " for: " + monsterDmg + " left: "+ player.getHealth());
}
gScreen.updateBot();
Thread.sleep(3000);

}

当我编写这个代码时,我认为这很酷,但我看到一些人创建了一个类只是为了控制线程本身。我刚刚使使用 Sleep 的类可运行(代码中未显示,但它是一个大类)。

最好能弄清楚这一点,这样我就可以在他们问我这件事之前指出我,你知道拿走那里的弹药。 :D

希望你们能帮助我:)。

谢谢

最佳答案

线程数超过 4 states 。另外,我建议阅读Lesson: Concurrency有关线程的更多信息。

请注意,如果您希望按设定的时间间隔执行任务,我强烈建议使用 Executors框架。

关于java - Java 中的线程,状态?另外,正确的使用方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8879643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com