Intro of JAVA
Learn Java
class Multithreadingexample extends Thread
{
public void run(){
System.out.println("Thread is created successfully");}
public static void main(String args[]) {
Multithreadingexample t1=new Multithreadingexample();
t1.start();
}
}
OUTPUT :-
class Multithreadingexample implements Runnable
{
public void run() {
System.out.println("Thread is created successfully"); }
public static void main(String args[]) {
Multithreadingexample m1=new Multithreadingexample();
Thread t1=new Thread(m1);
t1.start();
}
}
OUTPUT :-
class Sleepexample extends Thread{
public void run(){
for(int i=1;i<10;i++){
i=i+1;
try{Thread.sleep(500);}
catch(InterruptedException e)
{System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
Sleepexample t1=new Sleepexample ();
Sleepexample t2=new Sleepexample ();
t1.start();
t2.start();
}
}
OUTPUT :-
class Joinexample extends Thread{
public void run(){
for(int i=1;i<=10;i++){
i=i+1;
try{Thread.sleep(500);}
catch(InterruptedException e)
{System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
Joinexample t1=new Joinexample();
Joinexample t2=new Joinexample();
Joinexample t3=new Joinexample();
t1.start();
try{
t1.join();}
catch(Exception e){
System.out.println(e);
}
t2.start();
t3.start();
}
}
OUTPUT
class Nameexample extends Thread{
public void run(){
System.out.println("Thread is created successfully");
}
public static void main(String args[]){
Nameexample t1=new Nameexample();
Nameexample t2=new Nameexample();
System.out.println("Name of Thread 1 is:- "+t1.getName());
System.out.println("Name of Thread 2 is:- "+t2.getName());
t1.start();
t2.start();
t1.setName("qwe");
t2.setName("asd");
System.out.println("After changing the name of thread 1:- "+t1.getName());
System.out.println("After changing the name of thread 2:- "+t2.getName());
}
}
OUTPUT
Note:
Default priority of a thread is 5 (NORM_PRIORITY).
The value of MIN_PRIORITY is 1.
The value of MAX_PRIORITY is 10.
class Prioritythread extends Thread{
public void run(){
System.out.println("running thread name is:"+Thread.currentThread().getName());
System.out.println("running thread priority is:"+Thread.currentThread().getPriority());
}
public static void main(String args[]){
Prioritythread m1=new Prioritythread();
Prioritythread m2=new Prioritythread();
m1.setPriority(Thread.MIN_PRIORITY);
m2.setPriority(Thread.MAX_PRIORITY);
m1.start();
m2.start();
}
}
OUTPUT
Example of Daemon Thread in Java:-
public class DaemonThread extends Thread{
public void run(){
if(Thread.currentThread().isDaemon()){
System.out.println("daemon thread work");
}
else{
System.out.println("user thread work");
}
}
public static void main(String[] args){
DaemonThread t1=new DaemonThread();
DaemonThread t2=new DaemonThread();
DaemonThread t3=new DaemonThread();
t1.setDaemon(true);
t1.start();
t2.start();
t3.start();
}
}
OUTPUT:-
Example of Synchronization in Java:-
class Table{
synchronized void printTable(int n){
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
}
}
}
public class SynchronizationThread{
public static void main(String args[]){
final Table obj = new Table();
Thread t1=new Thread(){
public void run(){
obj.printTable(5);
}
};
Thread t2=new Thread(){
public void run(){
obj.printTable(100);
}
};
t1.start();
t2.start();
}
}
OUTPUT:-