Skip to content

Instantly share code, notes, and snippets.

@saurav-bhagat
Created April 8, 2019 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saurav-bhagat/3221a9f937017f3896d38c3554a89d30 to your computer and use it in GitHub Desktop.
Save saurav-bhagat/3221a9f937017f3896d38c3554a89d30 to your computer and use it in GitHub Desktop.
All basic Thread operations in java with example and for other a basic notes in comment.
/*
A java Program in which one thread will calculate factorial and other will check primarility.
*/
import java.util.*;
import java.lang.Thread;
class FactorialThread extends Thread{
private int n;
public FactorialThread(int n){
this.n = n;
}
public void run(){
Thread.currentThread().setName("Factorial"); //or t1.setName("") inside main
//we can set or get priority using t.setPriority() 1 to 10
System.out.println("Inside "+Thread.currentThread().getName()+ " And Factorial of "+ n+ " is:" + factorial(n));
}
int factorial(int n){
if(n<=1){
return n;
}
return n* factorial(n-1);
}
}
class PrimeThread extends Thread{
private int n;
public PrimeThread(int n){
this.n = n;
}
public void run(){
Thread.currentThread().setName("Prime"); //we could have also used t2.setName("") inside main
//or Thread.currentThread().setPriority(); default comes from parent thread
System.out.println("Inside "+Thread.currentThread().getName()+ " And Primarility of "+ n+ " is:" + isPrime(n));
}
boolean isPrime(int n){
if(n<=1){
return false;
}
boolean flag = true;
for(int i=2;i<n/2;i++){
if(n%i==0){
flag = false;
break;
}
}
return flag;
}
}
public class ThreadDemo1{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
int n = sc.nextInt();
FactorialThread factT = new FactorialThread(n);
PrimeThread pT = new PrimeThread(n);
factT.start();
pT.start();
}
}
/* ------ Thread Hold Methods -----
1. Thread.Yield() - When a thread calls this function, it will go into waiting state and
all other threads with same priority will execute before it.
2. Thread.join() - When t1 thread wants to execute after completion of t2, then t1 can
call t2.join
3. Thread.sleep() - We can just hold the execution of current thread for some time.
Except first, these methods can take milliseconds as first parameter and also both are checked
exceptions, hence we need to handle it either using try catch, or using throws keyword.
Note: We can pass reference of main thread into child thread by declaring a static variable
inside thread and storing Thread.currentThread() inside it. By, this we can make the
child thread wait till main thread completes.
If a Thread calls join method on itself, then deadlock.
Synchronization :
If multiple threads are trying to operate simultaneously on same java object, then inconsistency
can occur. If a method or block is declared as "synchronized", then at a time only one thread is
allowed to execute that method or block on a given object. (Internally implemented using concept
of locks). Keep in mind for this, there should be a single object and more than one threads and
there should be atleast one method in that object that should required synchronization.
If only a particular portion of code inside a method needs synchronization, then we can put that
block inside synchronized block.
InterThread Communication :
2 threads can communicate with each other by using wait(), notify() and notifyAll() method. Now,
bring in mind producer consumer problem and relate this.
The thread which is expecting updation is responsible to call wait() method and immediately enters
into waiting state, and The thread which will perform updation after performing updation, it is
responsible to call notify() method. Then waiting thread will get notification and continue its
execution with those updated items.
These methods are present inside object class and not Thread class, since these can be called upon
any valid java Objects.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment