Implementing Java Thread Pool in AS, Passing Parameters

Need help with SmartFoxServer? You didn't find an answer in our documentation? Please, post your questions here!

Moderators: Lapo, Bax

Zelcer
Posts: 17
Joined: 26 Jan 2013, 04:46

Implementing Java Thread Pool in AS, Passing Parameters

Postby Zelcer » 24 Sep 2018, 17:36

Hello,

I'm currently attempting to implement a java thread pool in my AS code, so that certain requests in the handleRequest function can be threaded. Currently, my code is looking like this:

inside the init function:

Code: Select all

_global.threadPool = new java.util.concurrent.Executors.newFixedThreadPool(10);

handleMovement function:

Code: Select all

function handleMovement(){
   //do some code here that references the handleRequest 'params' and 'user' parameters
}

handleRequest:

Code: Select all

function handleRequest(cmd, params, user, fromRoom, protocol){
   if(cmd == "m"){//this needs to be threaded
      _global.threadPool.execute(handleMovement);
   }
   //...
}


So the thread pool is working correctly, but my question is what would be the best way to give the handleMovement function access to the parameters in the handleRequest function. Is it possible to pass parameters through the execute function? Can this be done without creating a custom java class?

Thanks.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Implementing Java Thread Pool in AS, Passing Parameters

Postby Lapo » 25 Sep 2018, 14:18

Hi,
SFS 1.x uses Mozilla Rhino as Javascript runtime. Since Executor.execute(...) requires a Runnable instance I think it's possible to pass the parameters you need.

Here are several examples from the Rhino doc:
https://developer.mozilla.org/en-US/doc ... Interfaces

I think something like this should work:

Code: Select all

// create "runnable"
var obj =
{
   run: function ()
   {
       // code...
   }    
}

// populate params
obj.param1 = x
obj.param2 = y
...

// Run in thread pool
executor.execute(obj)


Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
Zelcer
Posts: 17
Joined: 26 Jan 2013, 04:46

Re: Implementing Java Thread Pool in AS, Passing Parameters

Postby Zelcer » 25 Sep 2018, 17:01

Thanks for the help Lapo, that's exactly what I was looking for.

For anyone else reading this thread, the example Lapo provided works perfectly, as long as you create a runnable from the object.

Code: Select all

run = new java.lang.Runnable(obj);
executor.execute(run);
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Implementing Java Thread Pool in AS, Passing Parameters

Postby Lapo » 26 Sep 2018, 07:24

Thanks for the correction :)
Lapo

--

gotoAndPlay()

...addicted to flash games
Zelcer
Posts: 17
Joined: 26 Jan 2013, 04:46

Re: Implementing Java Thread Pool in AS, Passing Parameters

Postby Zelcer » 27 Sep 2018, 17:02

Hey again Lapo,

so I've been playing around with this and have threaded some parts of my code via this method

Code: Select all

function handleRequest(cmd, params, user, fromRoom, protocol){
   if(cmd == "m"){//thread this
      handleMovementObj.params = params;
      handleMovementObj.user = user;
      _global.threadPool.execute(handleMovementRun);
   }


Code: Select all

handleMovementObj = {
   run: function(){
      //do some code here referencing the params and user parameters
   }   
}
handleMovementRun = new java.lang.Runnable(handleMovementObj);


However it seems because every runnable uses the same object the parameters themself aren't thread-safe, so if they change in the middle of another thread executing the data can become corrupt/incorrect. I incorrectly assumed that the parameters would be passed as local variables and this issue wouldn't occur. Do you have any advice on how to make these thread safe? Or can you point me in the right direction so I can do some further research specifically on this topic? I've done some reading on the previous article you linked, but I can't seem to find any specifics on this issue.

Thanks again.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Implementing Java Thread Pool in AS, Passing Parameters

Postby Lapo » 27 Sep 2018, 17:10

You need to create a new Runnable for every task you submit to the Executor.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
Zelcer
Posts: 17
Joined: 26 Jan 2013, 04:46

Re: Implementing Java Thread Pool in AS, Passing Parameters

Postby Zelcer » 27 Sep 2018, 17:16

Hmm, I see. Would something this simple be enough to get rid of the concurrency issues, or would a new object also have to be made?

Code: Select all

function handleRequest(cmd, params, user, fromRoom, protocol){
   if(cmd == "m"){
      handleMovementObj.params = params;
      handleMovementObj.user = user;
      _global.threadPool.execute(new java.lang.Runnable(handleMovementObj));
   }


And do you think this would this be too much overheard for requests that are sent very frequently (Each client may send several a second, with the server handling 100s)? I apologize for all the questions.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Implementing Java Thread Pool in AS, Passing Parameters

Postby Lapo » 27 Sep 2018, 17:24

Yes it should work.

And do you think this would this be too much overheard for requests that are sent very frequently (Each client may send several a second, with the server handling 100s)? I apologize for all the questions.

Depends on the reason why all these requests need to run in a separate thread. Unless they are keeping the thread waiting for some I/O it would be a bad idea.

Also given the fast rate of these requests, if the Runnable was doing some I/O I would question the need to do it so often, as it is indeed quite unoptimized.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
Zelcer
Posts: 17
Joined: 26 Jan 2013, 04:46

Re: Implementing Java Thread Pool in AS, Passing Parameters

Postby Zelcer » 27 Sep 2018, 17:39

The runnable in the aforementioned example simply broadcasts a player's movement back to any clients that need to be updated about the change. This currently takes up a large % of the server's execution time because of the frequency of the updates & because of how many users need to be sent the changes. I understand the need to thread less frequent but more time-consuming operations, but overall these take less time than something like the movement updates. Any thoughts on threading/optimizing operations like this? Or do you think it's better to leave these operations in the main extension handler thread?
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Implementing Java Thread Pool in AS, Passing Parameters

Postby Lapo » 28 Sep 2018, 07:26

Sending data back to clients doesn't require that many threads. Usually 4-6 unless your machine has, say, 32 cores or more.
There's only so much that the CPU can do and throwing more threads at a non-blocking task (such as sending packets) is going to be counterproductive (higher memory usage and CPU due to thread context switching).

If your machine struggles with a high packet rate/high traffic it might be necessary to upgrade it to a more powerful hardware.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
Zelcer
Posts: 17
Joined: 26 Jan 2013, 04:46

Re: Implementing Java Thread Pool in AS, Passing Parameters

Postby Zelcer » 28 Sep 2018, 19:25

Hmm, I see. Thanks for the help.

One last unrelated question. Is it possible to get the size (in bytes) of a packet sent to the server via extension code?
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Implementing Java Thread Pool in AS, Passing Parameters

Postby Lapo » 29 Sep 2018, 08:41

Hi,
sorry no. You can do it via a packet/traffic analyzer though (e.g.wireshark)

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SmartFoxServer 1.x Discussions and Help”

Who is online

Users browsing this forum: No registered users and 52 guests