implement thread in java [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 7 years ago.
Improve this question
I want to maintain thread pool, and it should contain multiple jobs. Job has execute one after another

If you have a sequential execution of four jobs, you probably want to aggregate them into a single Runnable.
void run(){
job4(job3(job2(job1(inputs))));
}
It does not make much sense to schedule separate threads for the jobs (as only one of them can proceed at the same time).
You could submit that whole thing as one to an ExecutorService.

Related

Limit requests to x times a minute with y times a second [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 days ago.
Improve this question
I have an api which allows me to run 180 requests every minute. However, if I request more than 50 times in a second, I will get temporarily banned. I have a thread class which sends the api request and performs and action on it. Assuming the class is called ApiRequestThread, what would I do? This is in java
I have used a mix of double guava ratelimiter but I wanted to know if anyone had any better ideas

Safely edit the Database with multiple threads in java [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 1 year ago.
Improve this question
I am trying to learn multi-threading in java.
Suppose I have a very big application with 100 running threads trying to execute a synchronized method which inserts a row in database.
As, the method is synchronized so only one thread will get the lock for that method and rest 99 will wait.
Only 1 thread is able to edit the Database and rest will be waiting. It seems a slow process. As all the threads will be editing the DB one by one. Is there any other way or concept to safely edit the database in a faster way?
i will recommend u to read about isolation level in transaction to handle some cases in concurrent application https://en.wikipedia.org/wiki/Isolation_(database_systems), sometimes is handles by default.
if for isntance u only adding new rows in table u shouldn't care about it and remove synchronized

Need of SingleThreadPool in Java? [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 have a few questions regarding ExecutorService.
In what cases we should use newSingleThreadExecutor() than others and Why?
Can you tell me the real use case of having SnewSingleThreadExecutor()?
If we have a single thread either from (newSingleThreadExecutor() or newFixedThreadPool(1) or newCacheThreadPool(1)) Do we still need to check for Thread Safety?
Why do we need newSingleThreadExecutor() if we can already create a single thread using newFixedThreadPool(1)
When you don't want tasks to run in parallel because of common data.
Swing's Event Dispatch Thread. It is not called executor, but in fact it is, just its execute method is called invokeLater.
It depends on what data you access. If that data can be accessed outside the tasks running on this executor, then yes. It does not depend of how you built your executor.
We do not need. I don't know what SingleThreadPool do you mean - there is no such class in Java runtime libarary.

run and update dynamic array every x minute [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 7 years ago.
Improve this question
I have array with size of 80000, the array updates itself in infinity loop,
and has about 1000+/- hits per second in O(1).
i need to create function that run every one minute exactly and what the function does is go through all the dynamic array and update a particular field inside every cell in the array.
How can I create that function that running on dynamic array?
maybe with threads?
To create function that run every one minute you can use TimerTask for that
Whether to use Thread or not depends on your business logic
(How you are updating array ,can it be in background,is it updated from network(Web service call)?. etc).use Thread only you need to improve responsiveness of your proceess/application

how to implement parallel programming in Java [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 8 years ago.
Improve this question
which libraries we need for parallel programming in JAVA.
You simply start new threads, using the Runnable interface (or a lambda function, in Java 8) with the Thread constructor. Here's a really, really basic example:
Thread t = new Thread(() -> {
System.out.println("I'm in a different thread from the other code in this example");
});
t.start();
Then, of course, you have to handle all the issues around concurrency, which involves things like the synchronized keyword, various things (perhaps) from java.util.concurrent and its child packages, etc.
There's a Java tutorial on this here.

Categories

Resources