I am using red5 for mmo , and I am in trouble with syncronized methods , the methods which i used in MultiThreadedApplicationAdapter always blocking next request. Is it normal ? Because i am doing some database operations in these methods and this syncronized block is making my performance very poor. I decided to use quartz jobs to overcome this stuation, how ever this time clustered topolgy is making me confused . Could you please help me , is it a common solution to use quartz for this problem , is there any body to give me a smarter advice
Thank you
I want to make an addition to clear my question
After extending MultiThreadedApplicationAdapter and create my overriden class , I implemented
public boolean connect(IConnection conn, IScope scope, Object[] params) {
function and in this function I want to set user status Online (As you can see there is not any syncronized literal in this function however it is acting as there is.)
And I want to take user entity from database and then set online status and then save it back
In this part even if I dont use syncronized literal , next coming client is waiting previous completed. I feel like I must create another job with Quartz and make database operations in that thread however I am not sure this decrease my performance. Is there any other way to prevent this block , this seems to be a Red5 limitation ??
This is also mentioned in a blog http://ria101.wordpress.com/2010/03/09/red5-cabin-fever-advanced-scope-and-room-management/
Only one thread can invoke synchronized method of an object, an quartz won't change it. And from your post doesn't seem what you want to achieve.
Related
im getting a string from callback and storing in a variable call channel. it's fine now but when accessing the value outside the callback method it returns null why?
it's not accessable outside of that method.
callback is working fine while inside of callback method
but i
please help me
ch = new Channel(fuser.getUid(),userid);
ch.getChann();
ch.Back(new Channel.MyCallback() {
#Override
public void onCallback(String value) {
channel = value;
Log.e("Channel",channel);
}
});
channel+="1";
Log.e("Channel1",channel);
unfortunately, there's not all text visible on your screenshot. On Stackoverflow, it's generally preferred if you paste the source code directly into your question rather than sharing a screenshot. It's a bit more effort, at the same time, please consider that the people answering your question also take effort to help you - and by posting the source code as text, you make it easier for everyone to help you.
For your question, there's a couple of things that might happen:
the callback is never called - and therefore there's nothing written into that variable
the callback is not called immediately, but later (maybe by another thread). Therefore, you will find that the variable will not be set immediately after you pass that callback to the Back method, but maybe a few seconds or milliseconds later. That's a long time for a computer, so when you access the variable later, you might just be too early.
if there's threads involved, your code is not thread save. That means - it might also happen that channel=value and channel+="1" might happen at the same time, which could give you unpredictable results.
To solv the problem, you will need to trigger whatever action should happen after your callback at a time when you know that the callback has been called. I am no expert on Android; there might be listeners available for that. If not, then the simplest way would be to call the code that should happen after the callback was called from the callback itself (be aware, that this is most likely a bad practise in android as it might make the UI become unresponsive. To solve that, you will need to execute your code on a different thread than the UI thread)
I'm trying to make a call by using slack's sdk client in java in order to get user's id by using the email name. The slack client returns CompletableFuture object. I can get the user_name if i use get() method but as far as i understand, it's a synchronous function and it will make the application slower. Is there another way to make this call asynchronous?
public static String lookUpUserId(String email) throws ExecutionException, InterruptedException {
CompletableFuture<UsersLookupByEmailResponse> response = slackClient.usersLookupByEmail(r -> r
.email(email));
UsersLookupByEmailResponse data = response.get();
return data.getUser().getId();
}
I tried using supplyFunc and thenApply like this but it gives an error saying 'missing return statement' although i return a value. I'm just concerned about the performance and curious if there's a a better way to handle this. Thanks
response.thenApply(r->r.getUser().getId());
Since the call returns CompletableFuture, it is alredy asynchronous. Yes, get() method is a synchronous function. Using it will NOT make the application slower, it will make application consume more memory. Asynchronous access like
response.thenApply(r->r.getUser().getId());
looks correct.
The reason of error message is that the method lookUpUserId is declared as returning String and so must have a return staement. If you want to make it asynchronous, then declare it as returning CompletableFuture<String> and add return statement
return response.thenApply(r->r.getUser().getId());
I don't know much specifically about the slack api but some information can be found in this answer with regards to housing your function in a class that implements Runnable.
Make your `CompletableFuture` in the constructor and run your gets in `run()`.
In my opinion it is a best practice to process all of your api requests off of the main thread but you should know that running some single thing in a separate thread on one line and joining that thread back on the next is only going to add a little overhead without any performance advantages. If you are processing a batch of requests you should start each on independent threads with a for loop and join them all together after.
I'm also noticing that my referenced answer doesn't really cover thread joins for retrieving your results sooo you will probably also find this informative. And if you havn't learned about object oriented programming yet that's ok! you'll be writing your own classes in no time.
Edit: Ask for code if you need it, It's better if you write it yourself.
I am making an android app in which I am fetching data from internet and storing it in a ArrayList with custom adapter. Fetching data takes time and in that time next function runs on its own. I only want the next function to run when data is completely fetched. What can I do? I think it has to do something with threads kindly explain what threads are and how can we use them?
Let's say there are 2 functions
Function A
Function B
I only want the function B to run when function A has completed its task. is there anyway to do that?
There are lots of resources available online where you can obtain information on Threads in Java.
I highly recommend the official Java Documentation.
This Introduction isn't half bad either.
As for obtaining information in one method and then waiting until it is done to run the next, as #cHao said, just call the methods sequentially like this
A();
B();
Unless you already have multiple threads set up in your code, this should work just fine.
So I've got a simple Server class that creates an instance of a Listener class for every connection made to the "server". The Listeners run on their own thread.
I get that there might be a concurrency control problem when invoking methods on the Server class that alter files/variables. But what happens if 2 Listeners try to invoke a method that e.g. only returns some information about the server status?
Can the same Server instance handle 2 calls at the same time? Or will one of the listeners have to wait till the server is done executing the method of the first caller?
Thanks for any help you guys might be able to provide!
If the method is not synchronized, the server can handle the two calls concurrently.
But if they ask for status, this means that the status changes over time. And if it changes over time, then all accesses, read and write, to this status should be done in a synchronized way. Otherwise, the listener threads could see an obsolete value of the status.
So the method should be synchronized, or the status should be an AtomicXxx value, or it should be volatile. The best, and correct solution is hard to give without seeing the code and knowing how the status is read and modified.
For something like that, that I imagine doesnt change that often, I'd consider using a ReadWriteLock - so most of the time you can have multiple threads reading the status concurrently, and only have to block them when you want to update the value.
In my application which runs user submitted code[1] in separate threads, there might be some cases where the code might take very long to run or it might even have an infinite loop! In that case how do I stop that particular thread?
I'm not in control of the user code, so I cannot check for Thread.interrupted() from the inside. Nor can I use Thread.stop() carelessly. I also cannot put those code in separate processes.
So, is there anyway to handle this situation?
[1] I'm using JRuby, and the user code is in ruby.
With the constraints you've provided:
User submitted code you have no control over.
Cannot force checks for Thread.interrupted().
Cannot use Thread.stop().
Cannot put the user code in a process jail.
The answer to your question is "no, there is no way of handling this situation". You've pretty much systematically designed things so that you have zero control over untrusted third-party code. This is ... a suboptimal design.
If you want to be able to handle anything, you're going to have to relax one (or preferably more!) of the above constraints.
Edited to add:
There might be a way around this for you without forcing your clients to change code if that is a(nother) constraint. Launch the Ruby code in another process and use some form of IPC mechanism to do interaction with your main code base. To avoid forcing the Ruby code to suddenly have to be coded to use explicit IPC, drop in a set of proxy objects for your API that do the IPC behind the scenes which themselves call proxy objects in your own server. That way your client code is given the illusion of working inside your server while you jail that code in its own process (which you can ultimately kill -9 as the ultimate sanction should it come to that).
Later you're going to want to wean your clients from the illusion since IPC and native calls are very different and hiding that behind a proxy can be evil, but it's a stopgap you can use while you deprecate APIs and move your clients over to the new APIs.
I'm not sure about the Ruby angle (or of the threading angle) of things here, but if you're running user-submitted code, you had best run it in a separate process rather than in a separate thread of the same process.
Rule number one: Never trust user input. Much less if the input is code!
Cheers
Usually you have a variable to indicate to stop a thread. Some other thread then would set this variable to true. Finally you periodically check, whether the variable is set or not.
But given that you can't change user code , I am afraid there isn't a safe way of doing it.
For Running Thread Thread.Interrupt wont actually stop as sfussenegger mentioned aforth (thanks sfussenegger recollected after reading spec).
using a shared variable to signal that it should stop what it is doing. The thread should check the variable periodically,(ex : use a while loop ) and exit in an orderly manner.
private boolean isExit= false;
public void beforeExit() {
isExit= true;
}
public void run() {
while (!isExit) {
}
}