Process and Threading Analysis (iii) the three-thread combat simulation

Source: Internet
Author: User

Haven't updated this series of articles for a long time, then the book is connected to the previous article. The previous article has said that the small bastard is to do the game development, Light said do not practice fake Bashi, the following article on the use of Java thread to do a small demo of the demonstration good.

Since it is to do game development, the game background or to have, then choose the Three Kingdoms theme bar.

Since it is the Three kingdoms, then it is a troubled world, the army is essential.

Small bastard I like Liu Bei, then the enemy is naturally assumed to be Cao.

Here we use the Runnabel object to start the army thread to simulate the army's fighting behavior.

Used to create Army threads, mock Army battle public class Armyrunnable implements runnable{... Omit several words}armyrunnable Liubeiarmybehav = new armyrunnable (); Armyrunnable Caocaoarmybehav = new armyrunnable (); Thread liubeiarmy = new Thread (Liubeiarmybehav, "Liu Bei Army"); Thread caocaoarmy = new Thread (Caocaoarmybehav, "Caocao army");

  

Remember once heard a master planner said that the three countries play what? Play will be Bai. Then we have to introduce the generals ' threads to influence the whole battle.

public class Herothread extends thread{public  herothread (String name) {         super (name);    }    @Override public   Void Run () {         System.out.println (Thread.CurrentThread (). GetName () + "Come also!" ");         for (int i = 0; i < 5; i++) {             System.out.println (Thread.CurrentThread (). GetName () + "chop" + i + "person");         }    }     }    

  

Finally, there must be a battlefield to carry the battle. How else would generals and soldiers be able to exert their abilities?

public class Battlefield extends thread{...}

 

Well, the objects we need are generally clear, so let's start with the army.

Start with a new Java project named Threekindoms.

Once the project is created, create a package to host the code, and I'll name it com.fanyoy.threekindoms.

Next, we can get into today's theme, the creation of the Army class. Since the Runnable interface was implemented, we chose to select the runnable when we created the class.

The following is the implementation of armyrunnable.

Package com.fanyoy.threekindoms;/* * by fanyoycreative CHENJD */public class Armyrunnable implements Runnable {///Liu Bei and Caocao army Thread volatile Boolean attacking; @Overridepublic void Run () {//TODO auto-generated method Stubwhile (attacking) {// If no military order makes attacking false, the attack lasts 10 times for (int i = 0; i<10; i++) {System.out.println (Thread.CurrentThread (). GetName () + "No." + I + "several attacks");//Give up the CPU, you can not light yourself, the enemy must fight back AH is not. Thread.yield ();}} The end of the loop prompts the end of the attack System.out.println (Thread.CurrentThread (). GetName () + "End Battle");}}

  

Note that attacking uses the volatile modifier here to represent variables shared by several tasks in a multithreaded application, so that the thread can correctly read the values written by other threads. Because attacking is modified by an external thread to control the behavior of the army, it is as if the order was ordered by the general and not by the army itself.

With the army, what else do you need? General? No! Battlefield? Binggo!

Let's create a battlefield that hosts this battle.

As you can see, this time because battlefiled inherits from the Thread class, so we select Java.lang.Thread, our Battlefiled class is built. Here we implement the Main method of battlefiled, because we need to implement the test in the main method. Then in the battlefield of the Run method, instantiate Caocao and Liu Beijun thread, when the army thread started, then the task of the battlefield thread is over, in order not to affect the military thread, you need to make the battlefield thread hibernate first. When a certain condition is reached, mingjinshoubing, end the battle.

In the end, let's put the battle in the game.

Continue next week ~ ~

Process and Threading Analysis (iii) the three-thread combat simulation

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.