Use the RuPengGame engine package to create a game form such as Peng game engine package download address Thread Runnable ticket selling instance, rupenggamerunnable

Source: Internet
Author: User

Use the RuPengGame engine package to create a game form such as Peng game engine package Thread Runnable ticket selling instance and rupenggamerunnable

 

Package com. swift; import com. rupeng. game. GameCore; // import the game engine package

// Implement the Runnable interface public class Game_RuPeng implements Runnable {public static void main (String [] args) {GameCore. start (new Game_RuPeng (); // generate the game object and let the engine start it} // Override the run () method in the interface to control the game @ Override public void run () {GameCore. setGameSize (1024,768); // set the size of the game window GameCore. setGameTitle ("game window"); // set the game window title GameCore. pause (3000); // set the game window to stay for 3 seconds }}

Rupeng game engine package

Link: https://pan.baidu.com/s/1eSKo8KE password: n32z

The following example describes the implements Runnable interface.

Taking the ticket selling program as an example, the Thread class is used to complete:

Package com. swift; class SellTickets extends Thread {private int ticket = 10; public void run () {for (int I = 0; I <20; I ++) {if (this. ticket> 0) {System. out. println ("selling tickets: ticket" + this. ticket --) ;}} public static void main (String [] args) {SellTickets mt1 = new SellTickets (); SellTickets mt2 = new SellTickets (); sellTickets mt3 = new SellTickets (); mt1.start (); // each thread sells 10 tickets, with a total of 30 mt2.start () tickets (); // but there are actually only 10 tickets, and each thread sells its own mt3.start (); // Resource Sharing Failed }}

 

If Runnable is used, resources can be shared. The following is an example:

Package com. swift; class SellTickets2 implements Runnable {private int ticket = 10; public void run () {for (int I = 0; I <20; I ++) {if (this. ticket> 0) {System. out. println ("selling tickets: ticket" + this. ticket --) ;}} public static void main (String [] args) {SellTickets2 mt = new SellTickets2 (); new Thread (mt ). start (); // The same mt, but not in the Thread. If the same new Thread (mt) is used ). start (); // an instantiated object mt, and an exception new Thread (mt) will occur ). start ();}}

Although there are currently three threads in the program, a total of 10 tickets are sold. That is to say, using Runnable to implement multithreading can achieve resource sharing.

In java, You can implement multithreading in two ways. One is to inherit the Thread class, the other is to implement the Runnable interface, and the Thread class is defined in the java. lang package. As long as a class inherits the Thread class and overwrites the run () method in this class, it can implement multi-threaded operations. However, a class can only inherit one parent class, which is the limitation of this method.

 

Related Article

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.