Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

Java threads best programming practices

Pragya Keshap answered on February 20, 2023 Popularity 2/10 Helpfulness 1/10

Contents


More Related Answers

  • java how to get all threads
  • What are daemon threads in java?
  • What are the Thread-safe classes in Java Collections framework
  • threads in java
  • Java Thread Example Using the Thread Class
  • Thread limitations
  • java program to implement thread
  • How many types of threads are there in Java?

  • Java threads best programming practices

    0

    Use immutable classes: We should always prefer the immutable class in multithreading programming because immutable classes make sure that values are not changed in the middle of an operation without using synchronized blocks. For example, in an immutable class, such as java.lang.String, any modification on String, such as adding something or converting into uppercase, always creates another string object, keeping the original object unbroken.

    Use local variables: Always try to use local variables instead of an instance or class-level variables because local variables are never shared between threads.

    Use thread pool: Thread pool can reuse previously created threads and eliminate the time of thread creation, which improves the performance of the application.

    Use the synchronization utility: Here, we can use the synchronization utility instead of the wait and notify methods. The java.util.concurrent package provides better synchronization utilities, such as CycicBariier, CountDownLatch, Sempahore, and BlockingQueue. It is very easy to wait for five threads using CountDownLatch to complete its task instead of implementing the same utility using the wait and notify methods. It is also easier to implement the producer-consumer design with the help of BlockingQueue rather than the wait and notify methods.

    Use concurrent collections instead of synchronized collection: Concurrent collections are implemented with the new locking mechanism provided by the Lock interface and designed in such a way that we can take advantage of the native concurrency construct provided by the underlying hardware and JVM. Concurrent collections give more scalability and performance than their synchronized counterparts. ConcurrentHashMap provides better performance than synchronized HashMap or Hashtable classes if there are many updates and fewer reads concurrently.

    Minimize locking scope: We should always try to reduce the locking scope as much as possible because locking block will not be executed concurrently and it impacts the application's performance. We should always first try to use atomic and volatile variables to achieve our synchronization requirement if our requirement is not satisfied with them, and then we need to use the functionality provided by the Lock interface. We can also reduce the locking scope to use a synchronized block instead of the synchronized method.

    Use Java Executor framework: It provides an abstraction layer on the Java threading framework and provides better control in terms of creating and executing threads in a multithreaded environment. 

    Popularity 2/10 Helpfulness 1/10 Language java
    Source: Grepper
    Link to this answer
    Share Copy Link
    Contributed on Feb 20 2023
    Pragya Keshap
    0 Answers  Avg Quality 2/10


    X

    Continue with Google

    By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
    X
    Grepper Account Login Required

    Oops, You will need to install Grepper and log-in to perform this action.