• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Java Multithreading

Thread Synchronization in Java

By Dinesh Thakur

When more than one thread has to use a shared resource, Java finds a way of ensuring that only one thread uses the resources at one point of time; this is called synchronization.

In Java, each object is associated with a lock. The term lock refers to the access granted to a particular thread that has entered a synchronized method. Monitor refers to a portion of the code in a program. It contains one specific region (related to data or some resource) that can be occupied by only one thread at a time. This specific region within a monitor is known as critical section. A thread has exclusive lock from the time it enters the critical section to the time it leaves. That is, the data (or resource) is exclusively served for the thread, and any other thread has to wait to access that data (or resource). Some common terminology while using monitors is as follows: entering the critical section is known as acquiring the monitor, working with the critical section (data or resource) is known as owning the monitor and leaving the critical section is known as releasing the monitor. [Read more…] about Thread Synchronization in Java

Thread Life Cycle in Java

By Dinesh Thakur

A thread can undergo some states during its life cycle. It is because in a multithreaded environment when multiple threads are executing only one thread can use the CPU at a time, and all other threads should be in some other states either waiting for their turn for the CPU or waiting for some other condition to be satisfied. [Read more…] about Thread Life Cycle in Java

Creating Threads in Java

By Dinesh Thakur

In Java, threads are objects and can be created in two ways:

1. by extending the class Thread

2. by implementing the interface Runnable

In the first approach, a user-specified thread class is created by extending the class Thread and overriding its run () method. In the second approach, a thread is created by implementing the Runnable interface and overriding its run () method. In both approaches the run () method has to be overridden. Usually, the code that is to be executed by a thread is written in its run () method. The thread terminates when its run () method returns. [Read more…] about Creating Threads in Java

Sleep Method in Thread in Java Example

By Dinesh Thakur

[Read more…] about Sleep Method in Thread in Java Example

Java Example for Join Thread

By Dinesh Thakur

[Read more…] about Java Example for Join Thread

Java isAlive() Example

By Dinesh Thakur

[Read more…] about Java isAlive() Example

Interthread Communication in Java Example

By Dinesh Thakur

Multithreading replaces event loop programming by dividing the tasks into discrete and logical units. Threads also helps in avoiding polling. Polling is usually implemented by a loop that is used to check some condition repeatedly. Once the condition is true, appropriate action is taken, this results in wastage of CPU time. Suppose that the producer has to wait until the consumer is finished before it generates more data. In a polling system, the consumer would waste many CPU cycles while it waits for the producer to produce. Once the producer has finished, it would start polling, wasting more CPU cycles waiting for the consumer to finish, and so on. [Read more…] about Interthread Communication in Java Example

Synchronization in Java Example

By Dinesh Thakur

Since multiple threads run independently without any communication between them, some problems may arise because of this asynchronous behavior of threads. Sometimes, we need an operation to be performed by only one thread at a time. Performing the same task by two threads simultaneously may lead to inconsistent results. So to ensure that two thread don’t execute the conflicting operations together and to initiate communication among thread, a process called synchronization is used. [Read more…] about Synchronization in Java Example

Thread Priorities Example in Java

By Dinesh Thakur

Thread priorities are used by the thread scheduler to decide when each thread should be allowed to run. Higher priority threads get more CPU time than lower priority threads. A higher-priority thread can also preempt a lower-priority one. For instance, when a lower-priority thread is running and a higher-priority thread resumes (from sleeping or waiting on I/O, for example), it will preempt the lower priority thread. [Read more…] about Thread Priorities Example in Java

Four Threads in Java Example

By Dinesh Thakur

[Read more…] about Four Threads in Java Example

isAlive() and Join() Example in Java By Inheriting Thread Class

By Dinesh Thakur

[Read more…] about isAlive() and Join() Example in Java By Inheriting Thread Class

isAlive() and Join() Example in Java By Implementing Runnable Interface

By Dinesh Thakur

[Read more…] about isAlive() and Join() Example in Java By Implementing Runnable Interface

isAlive() Example in Java

By Dinesh Thakur

To determine whether a thread has finished. we can call isAlive() on the thread. This method is defined by Thread and its syntax is :

final boolean isAlive()

This method returns true if the thread upon which it is called is still running otherwise it returns false.

[Read more…] about isAlive() Example in Java

Two Threads in Java by Implementing Runnable Interface

By Dinesh Thakur

[Read more…] about Two Threads in Java by Implementing Runnable Interface

Multiple Threads in Java by Inheritance

By Dinesh Thakur

[Read more…] about Multiple Threads in Java by Inheritance

Multiple Threads in Java by Implementing Runnable Interface

By Dinesh Thakur

[Read more…] about Multiple Threads in Java by Implementing Runnable Interface

Multiple Threads in Java Example

By Dinesh Thakur

We first make a class MultipleThreads which extends Thread class and then we create an instance of that class t1 and pass the string “Child Thread” as parameter to it which assigns the name of the thread as Child Thread and also displays the information of the child thread on the screen. After that the start() method is invoked on that object which initiates the run() method. Before executing run() method of MultipleThreads, control returns to main(). In main() method, the for loop is executed which is supposed to print Mainthread : 1 [Read more…] about Multiple Threads in Java Example

Implementing Runnable Interface in Java Example

By Dinesh Thakur

Another method to create a thread is to create a class that implements the Runnable interface. Runnable abstracts a unit of executable code. We can construct a thread on any object that implements Runnable. To implement Runnable, a class need only implement a single method called run(). [Read more…] about Implementing Runnable Interface in Java Example

Extending Thread Class in Java Example

By Dinesh Thakur

One way to create a thread is to create a new class that extends Thread, and then to create an instance of that class. The extending class must override the run() method, which is the entry point for the new thread. It must also call start() to begin execution of the new thread. [Read more…] about Extending Thread Class in Java Example

currentThread() in Java Example

By Dinesh Thakur

In this Java Example, a reference to the current thread (the main thread) is obtained by calling currentThread(), and this reference is stored in a local variable t. Then, we display the information about the thread. After that, a loop counts from 0 to 10, pausing one second between each line. The pause is accomplished by the sleep() method. The argument to sleep() specifies the delay period in milliseconds. The sleep() method might throw an InterruptedException if some other thread interrupts the sleeping one. [Read more…] about currentThread() in Java Example

Daemon Threads in Java Examples

By Dinesh Thakur

Java has two types of threads : user thread and daemon thread. [Read more…] about Daemon Threads in Java Examples

Thread Priority in Java Example

By Dinesh Thakur

Every thread in Java has a priority that helps the thread scheduler to determine the order in which threads scheduled. The threads with higher priority will usually run before and more frequently than lower priority threads. By default, all the threads had the same priority, i.e., they regarded as being equally distinguished by the scheduler, when a thread created it inherits its priority from the thread that created it. However, you can explicitly set a thread’s priority at any time after its creation by calling its setPriority() method. This method accepts an argument of type int that defines the new priority of the thread. Its syntax is. [Read more…] about Thread Priority in Java Example

Thread Pools in Java Example

By Dinesh Thakur

When a large number of threads are created, degrades the performance of your application. So Java provides a technique of thread pooling to solve this problem.

The thread pooling allows you to reuse a single thread repeatedly instead of creating a new thread for each task and allowing it to be destroyed when the task completes. It avoids the overhead associated with creating a new thread by maintaining a pool of available threads and retrieving one from the pool when necessary. [Read more…] about Thread Pools in Java Example

Explicit Locks in Java Example

By Dinesh Thakur

In order to apply locks explicitly, an instance of the Lock interface needs to be created which is defined in the java.util.concurrent. Locks package. Using the Lock interface is similar to using the synchronized keyword. The Lock interface provides lock () and unlock () methods to acquire and release the lock respectively. [Read more…] about Explicit Locks in Java Example

Method Level Synchronization in Java Example

By Dinesh Thakur

Method level synchronization prevents two threads from executing method on an object at the same time. A method can be synchronized by using the synchronized keyword as a modifier in the method declaration. Its syntax is a follows, [Read more…] about Method Level Synchronization in Java Example

Join() Method in Java Example

By Dinesh Thakur

final void join () – The isAlive () method occasionally useful as continuously checking the value returned by this method and then sleeping makes a very inefficient use of the CPU time. So a better alternative to wait for a thread to die is to use join () method of the Thread class. [Read more…] about Join() Method in Java Example

isAlive() in Java Example

By Dinesh Thakur

In order to make sure that the main thread finishes last, we introduced a long enough delay by calling the sleep () method within the main() method so that all other threads finish before the main thread. But this of course is hardly a satisfactory solution because if your estimate for the time delay is too short then other threads could finish after the main thread finishes. Therefore, the Thread class provide method isAli ve ()  to determine if a thread has ended . [Read more…] about isAlive() in Java Example

Creating Thread Using Runnable Interface in Java Example

By Dinesh Thakur

Another way to create a thread in Java is to define a class that implements the Runnable interface. The previous technique of creating threads by extending the Thread class does not work when you want a class that extends another class and can also run as a thread. It is because java supports only single inheritance, i.e., does not allow a class to extend more than one class. So in such a case implement the Runnable interface. The Runnable interface declares only one method run () that contains the code executed when the thread started. Therefore, if you define a class that implements the Runnable interface, then it must implement the run () method. [Read more…] about Creating Thread Using Runnable Interface in Java Example

Constructors and Methods of the Thread Class in Java with Example

By Dinesh Thakur

In this Example, we shows how the constructors and methods of the Thread class are used. Here, we have created a subclass MyThread that extends Thread class. This class consists of a field which stores the time in milliseconds for which the thread will sleep. It also contains a parameterized constructor that contains two parameters str of String type and d of int type. This constructor calls the superclass constructor that sets the Thread’s name to the value passed in str and delay field is set to value passed in d. [Read more…] about Constructors and Methods of the Thread Class in Java with Example

Java Thread | Creating Threads and Multithreading in Java

By Dinesh Thakur

Java Thread: One of the dominant features of the Java language is that it provides built-in support for multithreading – the concurrent running of multiple threads within the same program. Creating a thread in Java is relatively easy. Unlike the old fashioned programming languages, where you have to invoke system-dependent procedures and functions to implement multithreading, in Java, it is no harder than creating an instance of other classes. [Read more…] about Java Thread | Creating Threads and Multithreading in Java

Next Page »

Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW