Best way to run two task independently. [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
What is the best way run two block of code independently and join before third block.
Actually I m planning to design custom java based work flow.
And my requirement is to run two process independently and join at third process.
I don't want to create thread manually and join on it.
Any help is appreciated.

Are you using java 8? CompletableFuture is very handy:
import static java.util.concurrent.CompletableFuture.allOf;
import static java.util.concurrent.CompletableFuture.runAsync;
....
allOf(
runAsync(() -> {
// do first thing
}),
runAsync(() -> {
// do second thing
})
).join();
// do third thing
There are a lot of options, of course. For one, if you are doing IO, you would probably want to supply an executor to runAsync.

Related

Will having a lot of classes to process each command separately lag my pc? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Is having many classes for each command and process them a non-efficient way to run, or is it negligible and I can just leave it to do the way it does.
If you use a command management method, or something like JDAUtils, it shouldn't make it much slower, but generally if you start you classes something like this
if (event.getAuthor().isBot())return;
String prefix = //some reference;
String exact = event.getMessage().getContentRaw();
String lower = exact.toLowerCase();
String[] arguments = message.split(" ");
and check for processes this demands resources when you run a lot of classes
If you don't have a separate class for every command this shouldn't matter.
AFAIK try JDA Utils it makes managing simpler

How to properly implement Multi-threading in a JavaFX MVMM application [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm looking for a way to thread the ViewModels in a MVVM Architecture, without having to use tasks everywhere.
My ideal solution would be :
1 Application Thread (with the view)
1 Background Thread (with the viewModel)
Message passing between the 2
ViewModels still behave as normal Java Classes w/o Tasks/Service/Platform.runlater in every method
Is there a way to do this without bloating the code ?
This is probably not going to work the way you would like it to work. The view-models normally communicate with the views via bound properties. Therefore the properties of the view-models must always be updated on the application thread.
The proposed JavaFX way of dealing with concurrent background activities is to use Services. But if you don't like that it maybe worth it to have a look here: https://github.com/ReactiveX/RxJavaFX

Run combination of methods based on user input? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Assuming I have 4 methods
methodA()
methodB()
methodC()
methodD()
Now I want the user to be able to input a number to run a combination of the methods.
So the user can choose if they want to run:
methodA() and methodB()
or:
methodB(), methodC() and methodD()
or just one of the methods or all of them.
Is there any other possibility than specifying any possible combination in if-statements?
You could use a bitmask. In that way one number could represent multiple flags that indicate which methods get called. A quick google search returned the following:
http://drumcoder.co.uk/blog/2010/jan/06/bitmasks-java/ Explains some solutions for this problem, which is what I think you're facing.
If you need to store multiple boolean flags for something, you can do
it using bits and storing that inside a single integer.

I'm new at programming. I'm not sure how much to use methods [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
So, I just finished my first program, but I haven't use any methods in it, since I'm just beginning to learn how to use them. Here's the original code : http://codepad.org/JiBfJI8Q I started to fractionate it but realised that it would be a method inside another all the way down. Is that actually the way to do it, or did I get the idea wrong?
without having looked at your code:
The general idea of methods is to separate small
portions of code which might be used at multiple other places in your code.
so yes, calling methods from within other methods is a good thing to do.
ideally your so called "composed methods" read out like a little story:
public void transaction(){
openDatabaseConnection();
addRecordsToDatabase();
closeDataseConnection();
}

How to call a Thread in a method [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to programmatically move a Thread in another method. How I can do this?
that's entirely not possible in Java.
There are some ways around that - using tools like AtomicReferences, AtomicBoolean, wait/notify or Channels. With these tools, you could inform the other thread that it should do something specific.
Another approach would be to copy SwingUtilities invokeLater - like here: http://www.javamex.com/tutorials/threads/invokelater.shtml
However, I would like to ask the question why that method execution needs to be run in a specific thread? Wouldn't just another (new thread) be fine too? That should significally simplify your problem. In that case, just start a new thread to call that method

Categories