1.
Multithreading using thread class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package multithreading;
/**
* @author 23CS004
*/
class MultiThreading extends Thread {
/**
* @param args the command line arguments
*/
public void run(){
for(int i=1;i<=5;i++){
System.out.println(Thread.currentThread().getName()+" - "+i);
try{
Thread.sleep(500);
} catch(InterruptedException e){
System.out.println(e);
public static void main(String[] args) {
// TODO code application logic here
MultiThreading t1= new MultiThreading();
MultiThreading t2= new MultiThreading();
t1.setName("thread1");
t2.setName("thread2");
t1.start();
t2.start();
}
2.creating a thread using the runnable interface
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package myrunnable;
/**
* @author 23CS004
*/
class MyRunnable implements Runnable {
/**
* @param args the command line arguments
*/
public void run(){
for(int i=1;i<=5;i++){
System.out.println(Thread.currentThread().getName()+" - "+i);
try
Thread.sleep(500);
catch(InterruptedException e){
System.out.println(e);
}
public static void main(String[] args) {
// TODO code application logic here
Thread t1=new Thread(new MyRunnable(),"Thread1");
Thread t2=new Thread(new MyRunnable(),"Thread2");
t1.start();
t2.start();
}
3.demonstrating priority levels of threads
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package prioritydemo;
/**
* @author 23CS004
*/
class PriorityDemo extends Thread {
/**
* @param args the command line arguments
*/
public void run(){
System.out.println(Thread.currentThread().getName()+"-
priority:"+Thread.currentThread().getPriority());
public static void main(String[] args) {
// TODO code application logic here
PriorityDemo t1= new PriorityDemo();
PriorityDemo t2= new PriorityDemo();
PriorityDemo t3= new PriorityDemo();
t1.setName("low priority thread");
t2.setName("medium priority thread");
t3.setName(" high priority thread");
t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(Thread.NORM_PRIORITY);
t3.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.start();
t3.start();
}
4.demonstrating thread synchronization
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package sharedresource1;
/**
* @author 23CS004
*/
class SharedResource1 {
/**
* @param args the command line arguments
*/
synchronized void printNumbers(int n){
for(int i=1;i<=5;i++){
System.out.println(Thread.currentThread().getName()+" - "+(n*i));
try{
Thread.sleep(500);
catch(InterruptedException e){
System.out.println(e.toString());
class MyThread1 extends Thread{
SharedResource1 sr;
MyThread1(SharedResource1 pp)
sr=pp;
// this.sr=sr;
public void run(){
sr.printNumbers(5);
class MyThread2 extends Thread{
SharedResource1 sr;
MyThread2(SharedResource1 pp)
sr=pp;
this.sr=sr;
}}
public void run(){
sr.printNumbers(10);
public class SynchronizationDemo{
public static void main(String[] args) {
// TODO code application logic here
SharedResource1 pp= new SharedResource1();
MyThread1 t1=new MyThread1(pp);
MyThread2 t2=new MyThread2(pp);
t1.start();
t2.start();