Movatterモバイル変換


[0]ホーム

URL:


Open In App

In Java, concurrency enables multiple threads to execute simultaneously, thereby enhancing performance and efficiency. However, improper handling of shared resources can cause serious issues in program behavior.

Concurrency
Concurrency-Problem

Common Concurrency Problems

1. Race Condition

A race condition occurs when two or more threads access shared data simultaneously, and the outcome depends on the order in which the threads execute.

Java
classCounter{intcount=0;voidincrement(){count++;// Not atomic: multiple threads may interfere}}publicclassGFG{publicstaticvoidmain(String[]args)throwsInterruptedException{Counterc=newCounter();// Thread 1 increments count 1000 timesThreadt1=newThread(()->{for(inti=0;i<1000;i++)c.increment();});// Thread 2 increments count 1000 timesThreadt2=newThread(()->{for(inti=0;i<1000;i++)c.increment();});// Start thread 1t1.start();// Start thread 2t2.start();// Wait for thread 1 to finisht1.join();// Wait for thread 2 to finisht2.join();// May be < 2000 due to race conditionSystem.out.println("Final Count: "+c.count);}}

Output
Final Count: 2000

Explanation:

  • Both threads increment the shared count variable concurrently.
  • Since count++ is not atomic, intermediate updates can be lost, resulting in an incorrect final value.

Solution: Usesynchronized blocks or AtomicInteger to ensure thread-safe updates.

Java
// Using AtomicIntegerimportjava.util.concurrent.atomic.AtomicInteger;classCounter{AtomicIntegercount=newAtomicInteger(0);voidincrement(){count.getAndIncrement();}}

2. Deadlock

Adeadlock occurs when two or more threads are waiting for each other to release resources, causing all of them to be stuck forever.

Java
classGFG{privatefinalObjectlock1=newObject();privatefinalObjectlock2=newObject();publicvoidmethodA(){// Acquire lock1synchronized(lock1){System.out.println("Thread 1: Holding lock 1");// Waits for lock2synchronized(lock2){System.out.println("Thread 1: Holding lock 2");}}}publicvoidmethodB(){// Acquire lock2synchronized(lock2){System.out.println("Thread 2: Holding lock 2");// Waits for lock1synchronized(lock1){System.out.println("Thread 2: Holding lock 1");}}}publicstaticvoidmain(String[]args){GFGexample=newGFG();newThread(example::methodA).start();newThread(example::methodB).start();}}

Output
Thread 1: Holding lock 1Thread 1: Holding lock 2Thread 2: Holding lock 2Thread 2: Holding lock 1

Explanation:

  • Thread 1 acquires lock1 and waits for lock2.
  • Thread 2 acquires lock2 and waits for lock1.
  • Both threads are stuck, creating a deadlock.

Solution: Always get the locks in the same order in every thread. This way, threads won’t block each other.

Visibility Problem

This occurs when one thread modifies a variable, but other threads do not see the updated value due to caching or lack of synchronization.

Java
classGFG{// Thread-unsafeprivatebooleanrunning=true;voidstart(){newThread(()->{while(running){}System.out.println("Stopped!");}).start();}// Change may not be visible to other threadvoidstop(){running=false;}publicstaticvoidmain(String[]args)throwsInterruptedException{GFGt=newGFG();t.start();// Short pause before stoppingThread.sleep(100);// Thread may not see this without volatilet.stop();}}

Output: Time limit exceeded.

Explanation:

  • Thread may cache the running variable, so changes made by stop() may not be immediately visible.
  • This can cause the loop to never terminate.

Solution: Usevolatile keyword to ensure visibility:

private volatile boolean running = true;

This ensures that changes to running are immediately visible to all threads.

Starvation

Starvation occurs when a thread never gets CPU time or access to a resource because other high-priority threads keep executing.

Java
classGFG{publicstaticvoidmain(String[]args){RunnablelowPriorityTask=()->{while(true){System.out.println("Low priority thread running...");}};RunnablehighPriorityTask=()->{while(true){System.out.println("High priority thread running...");}};Threadlow=newThread(lowPriorityTask);Threadhigh=newThread(highPriorityTask);low.setPriority(Thread.MIN_PRIORITY);high.setPriority(Thread.MAX_PRIORITY);low.start();high.start();}}

Explanation:

  • High-priority thread consumes CPU aggressively.
  • Low-priority thread may get little or no execution time, leading to starvation.

Solution: Use fair locks or proper thread scheduling mechanisms to prevent starvation.


Improve
Improve

Explore

Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp