- package sgs;
- import java.util.concurrent.locks.ReentrantLock;
- import sgs.GameThread;
- public class playerThread implements Runnable{
- public playerThread(int i) {
- // TODO Auto-generated constructor stub
- this.num=i;
- }
- public final int num;
- GameThread gt;
- static int total=5;
- String role;
- public volatile int HP; ///血量;
- ReentrantLock plrlock;
- public int lastloop=-1;
- @Override
- public void run() {
- // TODO Auto-generated method stub
- int time=0;
- while(true){
- plrlock=new ReentrantLock();
- plrlock.lock();
- //synchronized gt;
- try{
- if(lastloop!=num&>.loop==num){
- System.out.println("第"+num+"个玩家开始出牌");////出牌动作
- lastloop=num;
- }
- else if(time>=10000000){
- time=0;
- gt.loop=this.get_next_player();//下一位玩家;
- try {
- Thread.currentThread().sleep(100000000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- finally{plrlock.unlock();}
- time++;
- //total++;
- /*try {
- Thread.currentThread().sleep(500);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }*/
- //total--;
- //System.out.println(Thread.currentThread().getName());//System.out.println(total);
- }
- }
- public int get_next_player(){
- return (gt.loop+1)%gt.player_num;
- }
- }
以上为玩家线程类,运行时出现问题:不能循环,顺序错误。
6/10 解决:顺序错误:
- try{
- if(lastloop!=num&>.loop==num){
- System.out.println("第"+num+"个玩家开始出牌");////出牌动作
- lastloop=num;
- while(true){
- if(time>=10000000){
- time=0;
- System.out.println("第"+num+"个玩家弃牌");
- gt.loop=this.get_next_player();//下一位玩家;
- /*try {
- Thread.currentThread().sleep(1000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }*/
- break;
- }
- time++;
- }
- System.out.println("下一个玩家为"+gt.loop+"号"+"lastloop为"+lastloop);
- }
循环错因:lastloop未设成static。。。。
时间: 2024-10-06 13:59:06