
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Thread Interference Error in Java
Let us see an example to understand the concept of Thread Interference error −
Example
import java.io.*; class Demo_instance{ static int val_1 = 6; void increment_val(){ for(int j=1;j<11;j++){ val_1 = val_1 + 1; System.out.println("The value of i after incrementing it is "+val_1); } } void decrement_val(){ for(int j=1;j<11;j++){ val_1 = val_1 - 1; System.out.println("The value of i after decrementing it is "+val_1); } } } public class Demo{ public static void main(String[] args){ System.out.println("Instance of Demo_instance created"); System.out.println("Thread instance created"); final Demo_instance my_inst = new Demo_instance(); Thread my_thread_1 = new Thread(){ @Override public void run(){ my_inst.increment_val(); } }; Thread my_thread_2 = new Thread(){ @Override public void run(){ my_inst.decrement_val(); } }; my_thread_1.start(); my_thread_2.start(); } }
Output
Instance of Demo_instance created Thread instance created The value of i after incrementing it is 7 The value of i after incrementing it is 7 The value of i after decrementing it is 6 The value of i after incrementing it is 8 The value of i after decrementing it is 7 The value of i after incrementing it is 8 The value of i after incrementing it is 8 The value of i after decrementing it is 7 The value of i after incrementing it is 9 The value of i after decrementing it is 8 The value of i after decrementing it is 7 The value of i after decrementing it is 6 The value of i after decrementing it is 5 The value of i after decrementing it is 4 The value of i after decrementing it is 3 The value of i after decrementing it is 2 The value of i after incrementing it is 3 The value of i after incrementing it is 4 The value of i after incrementing it is 5 The value of i after incrementing it is 6
A class named ‘Demo_instance’ defines a static value, and a void function ‘increment_val’, that iterates over a set of numbers, and increments it and displays it on the console. Another function named ‘decrement_val’ iterates over a set of numbers and decrements every time and displays the output on the console.
A class named Demo contains the main function that creates an instance of the class, and creates a new thread. This thread is overridden, and the run function is called on this object instance. Same thing is done for the second thread too. Both these threads are then called with the ‘start’ function.
- Related Articles
- User Thread vs Daemon Thread in Java?
- RNA Interference
- Destructive Interference
- Thread Pools in Java
- Daemon thread in Java
- How a thread can interrupt another thread in Java?
- Naming a thread in Java
- Inter thread communication in Java
- Demonstrate thread priorities in Java
- Change Thread Priority in Java
- Get current thread in Java
- Java Thread Priority in Multithreading
- Is Java matcher thread safe in Java?
- Proactive and Retroactive Interference
- Importance of Thread Priority in Java?

Advertisements