concept of multithreading in java [duplicate] - java

This question already has answers here:
How does Java handle multithreading?
(4 answers)
Closed 9 years ago.
While doing multi-threading programming in C we can assign threads to different cores of a processor and it gives us surety that the threads will be executed in different cores (i.e hyper-threading).But how exactly java does the above task--
Does it assign the threads to a single core and execute it in
time-stamp basis or assign to different cores..?
If it assign the above to different cores then how..?

By default, Java does not implement any form of Thread affinity. However, because it uses the underlying operating system's threads, it is possible to use native code to set the cpu affinity for a thread. One example of a project that does this is here: https://github.com/peter-lawrey/Java-Thread-Affinity

Related

Are Lists<T> thread safe to add/write to? [duplicate]

This question already has answers here:
Java ArrayList.add() method thread safe for purely parallel adding? [duplicate]
(6 answers)
Closed 3 years ago.
I want to add information to a List and that information is gathered from multiple threads. The threads do not need to pull data from the list. Is it safe to have a regular ArrayList or LinkedList? Or what other ways can I do this?
Regular collections are not thread safe, but there are ways to circunvent that:
You can use CopyOnWriteArrayLists, but they are very expensive to write, you can also create a Syncronized Collection, which are not so expensive to write, and you can also work with Queues, it will depend on what you want to achieve.

Pinging many devices simultaneously as a nonblocking thread in Java [duplicate]

This question already has answers here:
Ping multiple servers in Java [duplicate]
(2 answers)
How to ping an IP address
(16 answers)
Closed 5 years ago.
I want to ping many machines to obtain information of their reachability. Time is critical here and I dont want to wait 1-2 seconds for every machine (assuming that there are hundreds of them). I tried this code snippet and turns out that the isReachable method is synchronous.
InetAddress.getByName(host).isReachable(timeout);
I need a way to make either that method (if possible) nonblocking or come up with another solution that will help cut down the waiting time.
Any help would be greatly appreciated. Thanks in advance.

JVM track any objection creation [duplicate]

This question already has answers here:
How to intercept object creation in Java lower than user class level
(2 answers)
Closed 6 years ago.
Many times I've seen some people tracked how the VM creates objects and how it performs optimization (eg: String concatenation). Unfortunately I can't find the correct command. Does anyone know?
The best tool for this kind f investigation is to use a profiler. I would start with Flight Recorder to record object allocations and CPU consumption and where this is happening.

Thread Creation Difference [duplicate]

This question already has answers here:
"implements Runnable" vs "extends Thread" in Java
(42 answers)
Closed 8 years ago.
I have studied that we can create threads in two ways in java :
By extending Thread Class
By implementing Runnable interface under which we have to implement run()
Now my question is what is the difference b/w the two?
Is any 1 faster or efficient than other? something related to binding here or linking?
A Thread is a resource for performing work.
A Runnable is a unit of work.
Are you creating a new type of resource, or defining work? It's almost always the later.
In the simplest case there isn't really any functional performance difference. However, creating Runnables allows you to utilize Thread pools without changing your code much, which is a huge boost over using new Thread() in many cases.

Thread safe OutputStreamWriter [duplicate]

This question already has answers here:
Write to FileOutputStream from multiple threads in Java
(4 answers)
Closed 5 years ago.
Is there any thread-safe substitute for java.io.OutputStreamWriter in the JDK or some third party library?
None that I know of.
But you can use other means to effectively achieve thread-safety, like protecting the OutputStreamWriter with some monitor, Lock, or Semaphore. Also, you can use a single-threaded ExecutorService as a unique bottleneck through which other threads submit writing "jobs".
I answered this question: Write to FileOutputStream from multiple threads in Java
, which is exactly the same.
The short answer is no, but there are ways around it.

Categories

Resources