Package fengke.game.thread;
Import Java.util.PriorityQueue;
Import Javax.swing.plaf.SliderUI;
/**
* Using the thread yield () method to realize the peasant uprising in the Sui Dynasty
* @author Feng Ke
* Content: 1, create Army thread ===ruannable
* 2. Create class to hold military information
* 3, Kingdoms Army War
* 4. Creation of the show
*
*/
public class Stage {
public static void Main (string[] args) {
Create an army ======== "thread
Armyrunnable Armyrunnable_sui = new armyrunnable ();
Armyrunnable Armyrunnable_revolt = new armyrunnable ();
Thread Thread_armyrunnable_sui=new thread (Armyrunnable_sui, "Sui Dynasty army");
Thread Thread_armyrunnable_revolt=new thread (Armyrunnable_revolt, "rebel army");
Define the number of armies and military names of each army
int number_armyrunnable_sui=100;//number of the Sui Dynasty army
int number_armyrunnable_revolt=80;//number of rebels
To store military information in class information
Information Infor=new information ();
Infor.number_army=number_armyrunnable_sui;
Infor.number_revolt=number_armyrunnable_revolt;
Armyrunnable_sui.infor=infor;
Armyrunnable_revolt.infor=infor;
/*
* Start Fighting
* There is a difference between using start () and run ()
* The former process is used alternately, the latter executes the current run () and executes the next code;
*/
System.out.println ("The show is about to start: Please take your glasses and get ready!!!!" ");
try {
Thread.Sleep (2000);
} catch (Interruptedexception E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
}
War simulation
Thread_armyrunnable_sui.start ();
Thread_armyrunnable_revolt.start ();
try {
Thread.Sleep (2000);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
System.out.println ("Welcome to the next time!!!" ");
}
}
Package fengke.game.thread;
/**
* Army thread
*
* @author Front content: 1. Creating a military thread creation 2. Simulating war situations
*/
public class Armyrunnable implements Runnable {
Information infor;
@Override
public void Run () {
The following defined variables are called only once in each case and do not change the value
int number_armykilled = 0;
int number_revoltkilled = 0;
int number_army = Infor.number_army;
int number_revolt = Infor.number_revolt;
int number_army = Infor.number_army;
int number_revolt = Infor.number_revolt;
Switch (Thread.CurrentThread (). GetName ()) {
Case "Sui Dynasty army":
while (true) {
Infor.number_revolt <= 0 | | Infor.number_army <= 0 To determine whether the number of unilateral persons is 0
if (infor.number_revolt <= 0 | | infor.number_army <= 0) {
if (Infor.number_revolt <= 0) {
System.out.println ("Sui Dynasty Army victory!!! ");
SYSTEM.OUT.PRINTLN ("Accidental civilian" + (0-infor.number_revolt) + "person");
} else {
System.out.println ("Rebel Victory!!!" ");
SYSTEM.OUT.PRINTLN ("Accidental civilian" + (0-infor.number_army) + "person");
}
Break
}
int NUMBER_TEMP1 = 0;
Simulating the war situation of the Army
NUMBER_TEMP1 = (int) (Math.random () * 10);
System.out.println (Thread.CurrentThread (). GetName () + "launched a strong offensive," + "with" + (Infor.number_army) + "Strength"
+ "seriously wounded enemy" + NUMBER_TEMP1 + "People!!!" ");
Can be accumulated to accumulate the number of enemies killed
Infor.number_revolt = INFOR.NUMBER_REVOLT-NUMBER_TEMP1;
Thread.yield ();
}
Break
Case "Rebel army":
while (true) {
Record the number of kills this time
int number_temp = 0;
if (infor.number_revolt <= 0 | | infor.number_army <= 0) {
Break
}
number_temp = (int) (Math.random () * 10);
System.out.println (Thread.CurrentThread (). GetName () + "launched a strong offensive," + "with" + (Infor.number_revolt) + "Strength"
+ "seriously wounded enemy" + Number_temp + "People!!!" ");
Infor.number_army = infor.number_army-number_temp;
Thread.yield ();
}
Break
Default
System.out.println ("The end of the show!!! ");
Break
}
}
}
=============================================================================================
Package fengke.game.random;
Import Java.util.PriorityQueue;
Import Java.util.Random;
Import Javax.swing.plaf.SliderUI;
/**
* Using random number method to realize Sui and Tang kingdoms (not using yield)
*
* @author Feng Ke
* Content: 1, create Army thread ===ruannable
* 2. Create class to hold military information
* 3, Kingdoms Army War
* 4, the creation of the show
* Exception: An exception occurred when the thread was created using the extends thread method
* java.lang.illegalthreadstateexception=== the same thread cannot call the Start method repeatedly
*
*/
public class Stage {
public static void Main (string[] args) {
Create an army ======== "thread
Armyrunnable Armyrunnable_sui = new armyrunnable ();
Armyrunnable Armyrunnable_revolt = new armyrunnable ();
/*
* There is a problem with the created thread, which is mentioned in the following code
* Thread Thread_armyrunnable_sui = new Thread (Armyrunnable_sui, "Sui Dynasty army");
* Thread Thread_armyrunnable_revolt = new Thread (Armyrunnable_revolt, "rebel army");
*/
Define the number of armies and military names of each army
int Number_armyrunnable_sui = number of troops in 100;//Sui Dynasty
int number_armyrunnable_revolt = number of 80;//rebels
To store military information in class information
Information infor = new information ();//special storage of military information
Infor.number_army = Number_armyrunnable_sui;
Infor.number_revolt = Number_armyrunnable_revolt;
Armyrunnable_sui.infor=infor;
Armyrunnable_revolt.infor=infor;
Start the show.
System.out.println ("The show is about to start: Please take your glasses and get ready!!!!" ");
System.out.println ();
try {
Thread.Sleep (2000);
} catch (Interruptedexception E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
}
/*
* War Simulation content:
* Generate random numbers using the random class to make sure the army colorful
* Conditions for the end of the war: the number of unilateral armies is 0
*/
Random random = new random ();
while (Infor.number_revolt > 0 && infor.number_army > 0) {
if (Random.nextint (2) >0) {
Thread Thread_armyrunnable_sui = new Thread (Armyrunnable_sui, "Sui Dynasty army");
Thread_armyrunnable_sui.start ();
}else{
Thread Thread_armyrunnable_revolt = new Thread (Armyrunnable_revolt, "rebel army");
Thread_armyrunnable_revolt.start ();
}
try {
Thread.Sleep (200);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
/*
* Reasons for not using the following code:
* For a thread to say, he can only be started once, can not be repeated to start;
*
* while (Infor.number_revolt > 0 && infor.number_army > 0) {
* IF (Random.nextint (2) > 0) {
* Thread_armyrunnable_sui.start ();
*} else {
* Thread_armyrunnable_revolt.start ();
* }
* }
*/
System.out.println ();
System.out.println ("Welcome to the next time!!!" ");
}
}
Package fengke.game.thread;
public class Information {
public int number_army=0;
public int number_revolt=0;
}
Package fengke.game.random;
Import Java.util.Random;
/**
* Army thread
* @author Feng Ke
Content
* 1. Implement the creation of army threads
* 2. Simulation of war situations
*/
public class Armyrunnable implements Runnable {
Storing military information
Information infor;
@Override
public void Run () {
The following defined variables are called only once in each case and do not change the value
int number_armykilled = 0;
int number_revoltkilled = 0;
int number_army = Infor.number_army;
int number_revolt = Infor.number_revolt;
int number_army = Infor.number_army;
int number_revolt = Infor.number_revolt;
Switch (Thread.CurrentThread (). GetName ()) {
Case "Sui Dynasty army":
int NUMBER_TEMP1 = 0;
Simulating the war situation of the Army
NUMBER_TEMP1 for the death toll of the rebels, the random class produces a number of 0----(infor.number_revolt+1), preventing the last remaining of the two sides, causing a cycle of death.
NUMBER_TEMP1 = new Random (). Nextint (infor.number_revolt+1);
System.out.println (Thread.CurrentThread (). GetName () + "launched a strong offensive," + "with" + (Infor.number_army) + "Strength"
+ "seriously wounded enemy" + NUMBER_TEMP1 + "People!!!" ");
Can be accumulated to accumulate the number of enemies killed
Infor.number_revolt = INFOR.NUMBER_REVOLT-NUMBER_TEMP1;
Infor.number_revolt <= 0 | | Infor.number_army <= 0 To determine whether the number of unilateral persons is 0
if (infor.number_revolt <= 0 | | infor.number_army <= 0) {
if (Infor.number_revolt <= 0) {
System.out.println ("Sui Dynasty Army victory!!! ");
SYSTEM.OUT.PRINTLN ("Accidental civilian" + (0-infor.number_revolt) + "person");
} else {
System.out.println ("Rebel Victory!!!" ");
SYSTEM.OUT.PRINTLN ("Accidental civilian" + (0-infor.number_army) + "person");
}
}
Break
Case "Rebel army":
Record the number of kills this time
int number_temp = 0;
Number_temp1 for the Sui Dynasty Army death toll, the random class produced 0----(infor.number_army+1) number, prevent the last one of the two sides, resulting in a cycle of death
Random random=new random ();
Number_temp = Random.nextint (infor.number_army+1);
System.out.println (Thread.CurrentThread (). GetName () + "launched a strong offensive," + "with" + (Infor.number_revolt) + "Strength"
+ "seriously wounded enemy" + Number_temp + "People!!!" ");
Infor.number_army = infor.number_army-number_temp;
if (infor.number_revolt <= 0 | | infor.number_army <= 0) {
if (Infor.number_revolt <= 0) {
System.out.println ("Sui Dynasty Army victory!!! ");
SYSTEM.OUT.PRINTLN ("Accidental civilian" + (0-infor.number_revolt) + "person");
} else {
System.out.println ("Rebel Victory!!!" ");
SYSTEM.OUT.PRINTLN ("Accidental civilian" + (0-infor.number_army) + "person");
}
}
Break
Default
Break
}
}
}
Package fengke.game.random;
/**
* Storage of basic military information
* @author Feng Ke
*
*/
public class Information {
public int number_army=0;
public int number_revolt=0;
}
=============================================================================================
The process of realizing the peasant uprising in the Sui Dynasty: the difference between the yield of threads and the method of random discrimination