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

I made Django Blog App Tutorial

First 100,000 SIW Airdrop — Stay in Destiny World kicks off now!!

How to Create Child Theme in WordPress

Cucumber Reports

Using Brakeman prevents security incidents with Rails projects

And that’s how we cooked up a Mumbai-Andhra-Telangana style Yellow curry..?.. code..?

Egretia Engine Upgrade!

6 Easy Steps To Deploy Web Application On Kubernetes

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

Let’s talk about head first Java

Object-Oriented Programming in Java

Object Oriented Programming Concepts In Java

Java equals() and hashCode() contract