Important Concepts of Object-Oriented system (Part V)

Thread Lifecycle
//MultiThread.java
package com.thirstybrain.app.thread;
public class MultiThread extends Thread{ @Override
public void run() {
System.out.println("Hello from MultiThread class and Thread id is "+currentThread().currentThread().getId());
}
}
//ThreadProgram.java
package com.thirstybrain.app.thread;
public class ThreadProgram { public static void main(String[] args){
MultiThread multiThreadObj=new MultiThread();
multiThreadObj.start();
//Output : Hello from MultiThread class and Thread id is 8
}
}
  1. Extend Thread class
//MultiThread.java
package com.thirstybrain.app.thread;
public class MultiThread implements Runnable{ @Override
public void run() {
System.out.println("Hello from MultiThread class and Thread id is "+Thread.currentThread().currentThread().getId());
}
}
//ThreadProgram.java
package com.thirstybrain.app.thread;
public class ThreadProgram { public static void main(String[] args){
Thread thread=new Thread(new MultiThread());
thread.start();
//Output : Hello from MultiThread class and Thread id is 8
}
}
synchronized(sync_object){
// access shared resources
}
public static synchronized void increment() {
++counter;
}

--

--

--

Technical Consultant | Passionate about exploring new Technology | Cyber Security Enthusiast | Technical Blogger | Problem Solver

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Lining up Audio & Visual to Build Cutscenes in Unity

Understanding HashMaps in Java

Managed Magento 2 Hosting vs Magento Cloud Hosting: Which is Better?

Team Good Code

When Technologies Don’t Scale

How to fix image rotation based on exif metadata on PHP without Imagick?

Start Your Project With Python & Odoo

ASSURE DAO

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kumar Shivam

Kumar Shivam

Technical Consultant | Passionate about exploring new Technology | Cyber Security Enthusiast | Technical Blogger | Problem Solver

More from Medium

Dynamic JSON Translator

Java Project Structure.

Basics of MySQL InnoDB Cluster

Decode string

Git Git Git! One of The Most Important Utilities in History, Git Tips and Experience!