DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Enhancing Business Decision-Making Through Advanced Data Visualization Techniques
  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • Building an AI/ML Data Lake With Apache Iceberg
  • Beyond Simple Responses: Building Truly Conversational LLM Chatbots

Trending

  • Start Coding With Google Cloud Workstations
  • Why Database Migrations Take Months and How to Speed Them Up
  • The Future of Java and AI: Coding in 2025
  • Beyond Code Coverage: A Risk-Driven Revolution in Software Testing With Machine Learning
  1. DZone
  2. Data Engineering
  3. Data
  4. Dealing With the Disadvantages of Multithreading

Dealing With the Disadvantages of Multithreading

Two sides to every coin, a yin and its ever-pursuant yang. Every tool has its disadvantages, and multithreading is no exception.

By 
Dzung Tran user avatar
Dzung Tran
·
Jan. 03, 18 · Analysis
Likes (9)
Comment
Save
Tweet
Share
18.7K Views

Join the DZone community and get the full member experience.

Join For Free

When I stared to work on multithreading as a request from a customer, I heard a lot of good points about multithreading like improved performance, better use of system resources, better availability, among other things.

At that time, I thought this one is exactly which I was looking for. But one question came to me: multithreading sounds good, but what are disadvantages?

Image title

I found a list of disadvantages in my research. If we choose to use multithreading we have to know which problems will come to our project in the near feature, and how our project can live with them. When we know that, we can minimumize the effect of disadvantages to our project.

So, let’s see which we can do. Here are some of the main disadvantages of multitheading.

Difficulty of Writing Code

When you check the guideline to using threads, it’s so simple: it just extends and implements some functions. But it’s not really as simple as it appears, as you have to take care for a lot of things afterwards.

When you apply multithreading, you will push a lot of threads together at same time, on the same data, same objects, and same functions. If you don’t have a good way to control them, everything will become terrible. So that’s reason why we need expert person-to-design multithreading.

So, what can we do? 

Replace Any Function or Code That Are Not Thread-Safe

What’s thread-safe? Your source code can be called thread-safe if all threads go inside the same function at the same time, and each thread can get exactly the data which they expected not the data of other thread.

Image titleThere are many cases which make your source code not thread-safe, including static variables, static function, and singleton class, to name a few. If all threads run same time, they will use the same static variables or function, and this thread will get the data of another thread.

Three ways to make an object thread-safe are:

  • Synchronizing the critical sections. When you do that, only 1 thread can work with this part at 1 time, but take care when you use that, only for the source code which all thread can’t run together. This one will make the performance go down.

  • Use immutable objects. Immutable objects are simply objects whose state (the object’s data) can’t change after construction.

  • Use thread-safe wrappers. This means you put the main class (which isn’t thread-safe) inside a new class that is thread-safe. This way is helpful when the main class from third party or can’t update.

Ignore Deadlock

Image title

The deadlock happens when one thread is waiting for the resource of other thread, but the other thread still waiting for the resource which keep by the first thread.

To ignore the deadlock, we have to ensure that:

  • Each thread has to process different data.

  • Each thread have to create own object and function by themselves.

  • Don’t share the data between threads.

Careful When Using Synchronized

Inside multiple thread when we use this one, it will make the performance go down, you have to make it as small as you can, only use it when you have to use.

Image title

Difficulty of Testing and Debugging

With simple thread you can easily test and debug, and see the work flow of data inside your function, but if all threads run together, you can’t do it.

To deal with that, we can:

1. Keep the number of threads as a input parameter of main function if you can. And when something happens, we can set the number of threads to 1, and we have a single thread to test and debug.

However, this way just supports finding the business bugs, and the bugs of single thread. If we got the problem with multiple threads like each threads have conflict with each other, this way is not helpful.

2. Add useful log to the thread. The log is so powerful, try to put the log at the position which you can see the running flow, and you can see the whole flow. Write log when the exception happen, I hate to catch java. lang.Exception but if you want the thread keep going on the next data, you have to catch it, to make sure everything is under control.

At the first time running we can put a lot of log info to your source code, and when everything runs stably, you can remove them. But don’t make it too big.

Image title

3. Use the tool to test. When multiple threads are running, you can use the tool to see how the threads running, which one is running, which one is not, the status of each thread, and so on.

We have some tools here. I used Jconsole, which already supports Java 6.

You can read detail about it here.

You will get a lot of helpful information from this tool. To check the threads status, you can see the picture below.

Image title

With this GUI, you can see how many threads are running, when they stop, whether they are deadlock edor not, and more. It’s so helpful for testing purpose.

A coin has two sides, everything have good and bad points. Each technical tool will have advantages and disadvantages. If you can use the power of advantages and find the way to live with disadvantages, you've got that technical tool.

Let’s enjoy it!

Data (computing)

Opinions expressed by DZone contributors are their own.

Related

  • Enhancing Business Decision-Making Through Advanced Data Visualization Techniques
  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • Building an AI/ML Data Lake With Apache Iceberg
  • Beyond Simple Responses: Building Truly Conversational LLM Chatbots

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: