Threads in Java

A thread🧵
📌What are threads?
Threads allow a program to operate more efficiently by doing multiple things at the same time.
Every java application has at least one thread – the main thread. But from the application point of view – main is the first java thread and we can create multiple threads from it.

Multithreading refers to two or more threads executing concurrently in a single program
Threads can be used to perform complicated tasks in the background without interrupting the main program.
📌Life cycle of a thread
🔸New −
A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread
🔸Runnable −
After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task
🔸Waiting −
Sometimes, a thread transitions to the waiting state while the thread waits for another thread to perform a task. A thread transition back to the runnable state only when another thread signals the waiting thread to continue executing
🔸Timed Waiting −
A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs
🔸Terminated (Dead) −
A runnable thread enters the terminated state when it completes its task or otherwise terminates
📌Ways of creating threads
There are two ways of creating threads -
1. Extending Thread class
2. Implementing Runnable interface
🔸Extending Thread class -
We need to override the run() method in the Thread class. To start the thread we use the start() method on the thread object which executes a call to the run() method.
🔸Implementing Runnable Interface -
📌Thread priorities
Every Java thread has a priority that helps determine the order in which threads are scheduled.
By default, every thread is given priority NORM_PRIORITY (a constant of 5)

MIN_PRIORITY = 1
MAX_PRIORITY = 10
📌Different Thread methods
If you liked this, make sure to:

1. Follow me @meetjain74

2. Retweet the first tweet

3. Turn on the notifications to never miss these amazing tweets.

Thanks so much for getting to the end of the thread! Let me know your thoughts below

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Meet Jain🦅

Meet Jain🦅 Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @meetjain74

8 Jan
Programming concepts through memes😜

A thread🧵
📌Recursion Image
📌Doubly circular linked list Image
Read 13 tweets
7 Jan
How to choose a programming language?

A thread🧵
Many people ask me that 'Which programming language should I learn..?'

Well here are some programming languages with their uses. Choose yourself based on your interest.
📌Python -
🔸Artificial Intelligence
🔸Machine Learning
🔸Data analytics
🔸Data visualization
🔸Web development using the frameworks - Django, Pyramid, and flask
🔸Game development using python
🔸Python used in the field of Search Engine Optimisation (SEO)
Read 9 tweets
6 Jan
What is recursion?

A thread🧵 Image
“In order to understand recursion, one must first understand recursion.”
📌Fun Activity
Google 'recursion' and you will find that Google will ask 'Did you mean: recursion'. Despite clicking on recursion from spell checker – Did you mean, Google will keep on displaying it. Though the spelling is right, Google keeps on telling it again and again. Image
Read 18 tweets
4 Jan
Learning Data structures through diagrams

A thread🧵
🔸Arrays
🔸Stack
Read 10 tweets
3 Jan
Inheritance in Java

A thread🧵
📌What is inheritance?
When we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class (and avoid the writing of the same code again). This is referred to as inheritance.
📌Example Image
Read 7 tweets
29 Dec 21
Coding vs Programming

A thread🧵
As a programmer you might have come across both these words- coding and programming but do you know the actual difference between the two

Let us look at the difference between both these terms now and let me clear your doubts.
Read to the end of the thread!
📌Coding
Coding is basically the act of translating codes from human language to machine-based language. It can also be called a subset of programming since it is the foundation of programming
Read 11 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(