15/6/10 Sanguosha multi-player thread swap

Source: Internet
Author: User
Tags stub

  1. Package SGS;
  2. Import Java.util.concurrent.locks.ReentrantLock;
  3. Import SGS. Gamethread;
  4. public class Playerthread implements runnable{
  5. Public playerthread (int i) {
  6. TODO auto-generated Constructor stub
  7. This.num=i;
  8. }
  9. public final int num;
  10. Gamethread gt;
  11. static int total=5;
  12. String role;
  13. public volatile int HP; blood volume;
  14. Reentrantlock Plrlock;
  15. public int lastloop=-1;
  16. @Override
  17. public void Run () {
  18. TODO auto-generated Method Stub
  19. int time=0;
  20. while (true) {
  21. Plrlock=new Reentrantlock ();
  22. Plrlock.lock ();
  23. Synchronized gt;
  24. try{
  25. if (lastloop!=num&&gt.loop==num) {
  26. System.out.println ("+num+" players start to play);////the action of the card
  27. Lastloop=num;
  28. }
  29. else if (time>=10000000) {
  30. time=0;
  31. Gt.loop=this.get_next_player ();//next player;
  32. try {
  33. Thread.CurrentThread (). Sleep (100000000);
  34. } catch (Interruptedexception e) {
  35. TODO auto-generated Catch block
  36. E.printstacktrace ();
  37. }
  38. }
  39. }
  40. Finally{plrlock.unlock ();}
  41. time++;
  42. total++;
  43. /*try {
  44. Thread.CurrentThread (). Sleep (500);
  45. } catch (Interruptedexception e) {
  46. TODO auto-generated Catch block
  47. E.printstacktrace ();
  48. }*/
  49. total--;
  50. System.out.println (Thread.CurrentThread (). GetName ());//system.out.println (total);
  51. }
  52. }
  53. public int Get_next_player () {
  54. Return (gt.loop+1)%gt.player_num;
  55. }
  56. }

The above is the player thread class, the runtime problem: Can not loop, the wrong order.

6/10 FIX: Sequential error:

  1. try{
  2. if (lastloop!=num&&gt.loop==num) {
  3. System.out.println ("+num+" players start to play);////the action of the card
  4. Lastloop=num;
  5. while (true) {
  6. if (time>=10000000) {
  7. time=0;
  8. System.out.println ("+num+", "player fold");
  9. Gt.loop=this.get_next_player ();//next player;
  10. /*try {
  11. Thread.CurrentThread (). Sleep (1000);
  12. } catch (Interruptedexception e) {
  13. TODO auto-generated Catch block
  14. E.printstacktrace ();
  15. }*/
  16. Break
  17. }
  18. time++;
  19. }
  20. System.out.println ("The next player is" +gt.loop+ "" + "Lastloop for" +lastloop ");
  21. }

Loop error Cause: Lastloop not set to static ....

15/6/10 Sanguosha multi-player thread swap

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.