Simulate multiple threads with one thread to reduce the number of threads ~

Source: Internet
Author: User

In game development, there are inevitably many scheduled tasks and animation effects.

These cannot be completed in one process.

Under normal circumstances, I will think of using threads. But how can we use so many threads for so many scheduled tasks and so many animation effects ?~

 

So there is a stupid way to be stupid ~

Just simulate multiple threads on your own ~ Isn't it easy to execute a function at intervals ~

Let's see:

 

Public class gameeventthread extends thread {private final string tag = "gameeventthread"; private int sleep_time = 10;/* Note that a_gameevent is an event class, */private arraylist <a_gameevent> gameeventlist = new arraylist <a_gameevent> (); Private Boolean isrun = true; Public gameeventthread (string threadname) {super (threadname);} public gameeventthread (string threadname, int isleeptime) {super (threadname); this. sleep_time = isleeptime;}/*** Add the event to the thread's event list * @ Param event */Public void addgameevent (a_gameevent event) {If (gameeventlist. contains (event) = false) {gameeventlist. add (event) ;}} public void stoprun () {This. isrun = false ;}@ overridepublic void run () {iterator <a_gameevent> iter = NULL; a_gameevent event = NULL; while (isrun) {/* traverse the event list */try {iter = gameeventlist. iterator (); While (ITER. hasnext () {event = ITER. next ();/* event survival */If (event. isalive () {/* arrival event execution time */If (event. isatthetime () {event. doevent () ;}} else {/* event no longer alive. Delete this event */ITER. remove () ;}} catch (concurrentmodificationexception E1) {}try {thread. sleep (sleep_time);} catch (interruptedexception e ){}}}

This is a thread. It is easy to do. It is to traverse the event list and find that an event has reached the execution time, and then execute the callback function of the event ~

So, do you really want to ask me how to determine if the event has reached the execution time ?~

Come and listen to my practices:

Public abstract class a_gameevent implements I _gameevent {protected Boolean isalive = true; private long starttime = 0; private int isleeptime = 0; Public a_gameevent (INT _ isleeptime) {This. isleeptime = _ isleeptime; refreshstarttime ();}/* record start time */Public void refreshstarttime () {starttime = system. currenttimemillis ();}/* determines whether the time interval has been reached: the current time-the start time is later than the sleep time */Public Boolean isatthetime () {long endtime = system. currenttimemillis (); Long Span = endtime-starttime; If (span> = isleeptime) {starttime = endtime; return true;} return false;}/* isalive indicates whether the event is alive, if the event does not survive, it will be killed */Public void setisalive (Boolean isalive) {This. isalive = isalive ;}}

How is it? Is it easy? The current time minus the time when an event is added. If this time is greater than the preset event sleep time, the event will be executed ~

 

Hello ~! Wait. Why don't you ask me what the I _gameevent interface is?

Hey hey, this is it:

Public interface I _gameevent {public void doevent (); // execute the public Boolean isalive () operation; // determine whether the operation is alive}

Simple ~ Not explained ~

 

Try again ~

1. There are many event classes, which have a start time and sleep time.

2. There is a global thread that continuously traverses an event list.

3. The current time-when the event start time is greater than or equal to the event sleep time, the event is executed.

4. No ~ Finished ~

 

Alas ~ I know, this method may have been available for a long time ~

However ~ This is what I learned ~ I have not read any books or articles ~ No one asked ~

It is learned in the development process ~ Oh ~ No, you must like me ~

Okay, don't like it...

 

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.