0% found this document useful (0 votes)
13 views8 pages

Programs On16th Nov

Uploaded by

migivek884
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views8 pages

Programs On16th Nov

Uploaded by

migivek884
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

THREAD 1:

class UserThread extends Thread{


public void run(){
for(int i=1;i<5;i++){
try{
Thread.sleep(1000);
}catch(InterruptedException e){
System.out.println(e);
}
System.out.println(i);
}
}

}
public class TestThread1 {

public static void main(String[] args) {


UserThread obj=new UserThread();
UserThread obj2=new UserThread();
obj.start();
THREAD2.
class UserThread2 extends Thread{
String name="krishna";

public void run() {


for(int i=0;i<name.length();i++){
try{
Thread.sleep(1000);
}catch(InterruptedException e){
System.out.println(e);
}
System.out.println(name.charAt(i)+"
");
}
}
}
public class TestThread2 {

public static void main(String[] args) {


UserThread2 obj1=new UserThread2();
UserThread2 obj2=new UserThread2();
obj1.start();
try{
obj1.join();
}catch(Exception e){
System.out.println(e);
}
System.out.println();
obj2.start();
THREAD3.
class UserThread3 extends Thread{
public void run(){

System.out.println(Thread.currentThread().getName
());
}
}

public class TestThread3 {

public static void main(String[] args) {


UserThread3 obj1=new UserThread3();
UserThread3 obj2=new UserThread3();
obj1.setName("Java");
obj2.setName("Python");
obj1.start();
obj2.start();
}

}
THREAD4.
class UserThread4 extends Thread{
public void run(){
System.out.println("Thread
name:"+Thread.currentThread().getName()
+"priority:"+Thread.currentThread().getPriority()
);
}
}
public class TestThread4 {

public static void main(String[] args) {


UserThread4 t1=new UserThread4();
UserThread4 t2=new UserThread4();
UserThread4 t3=new UserThread4();
UserThread4 t4=new UserThread4();
t1.setName("t1");
t2.setName("t2");
t3.setName("t3");
t4.setName("t4");

t1.setPriority(Thread.NORM_PRIORITY);
t2.setPriority(Thread.MAX_PRIORITY);
t3.setPriority(Thread.MIN_PRIORITY);
t4.setPriority(Thread.NORM_PRIORITY);

t1.start();
t2.start();
t3.start();
t4.start();

}
THREAD5:
class UserThread5 extends Thread{
public void run(){
if(Thread.currentThread().isDaemon())
System.out.println("Daemon thead is
running");
else
System.out.println("Userthread is
running");
}

}
public class TestThread5 {

public static void main(String[] args) {


UserThread5 obj1=new UserThread5();
UserThread5 obj2=new UserThread5();

obj1.setDaemon(true);
obj1.start();
obj2.start();
}
THREADPOOL PROGRAM
import java.util.concurrent.ExecutorService;//interface

import java.util.concurrent.Executors;//class
class WorkerThread implements Runnable{
private String msg;
public WorkerThread(String msg){
this.msg=msg;

}
@Override
public void run() {

System.out.println(Thread.currentThread().getName()+"(
start)message="+msg);
processMsg();

System.out.println(Thread.currentThread().getNam
e()+"(end)");

}
private void processMsg(){
try{
Thread.sleep(1400);
}catch(Exception e){
System.out.println(e);
}
}
}
public class TestThreadPool {

public static void main(String[] args) {


ExecutorService
executor=Executors.newFixedThreadPool(5);//creating
pool of thread
for(int i=0;i<10;i++){
Runnable worker=new
WorkerThread(""+i);
executor.execute(worker);//callling
execute method of executor service

}
executor.shutdown();
while(!executor.isTerminated()){}
System.out.println("finished all
thread...............");
}

}
ExWithoutLambda:

interface Myinterface{
void show();
}
public class ExWithoutLambda {

public static void main(String[] args) {


int num=10;
//without lambda,we implement the interface
with annonymus class
Myinterface mi=new Myinterface(){
public void show(){
System.out.println(num);
}
};
Myinterface mi1=()->{
System.out.println(num);// no parameter
expression...
};
mi1.show();
}

ExLambdaWithParameter:

interface Printable{
public String someprint(String name);
}
public class ExLambdaWithParameter {

public static void main(String[] args) {


//lambda expression with single parameter
Printable p1=(name)->{
return"hello"+name;

};
System.out.println(p1.someprint("krishna"));

ExLambdawithParameter2:

import java.util.*;

interface Oddeven{
String check(int num1);
}
public class ExLambdawithParameter2 {

public static void main(String[] args) {


// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("Number: ");
int num=sc.nextInt();
Oddeven obj=(num1)->{
if(num%2==0)
return "even";
else
return "Odd";

};
System.out.println(obj.check(num));
}

ExLambda3:

public class ExLambda3 {

public static void main(String[] args) {


Runnable r1=new Runnable(){

@Override
public void run() {
System.out.println("thread is
running...");

};
Thread t1=new Thread(r1);
t1.start();

Runnable r2=()->{
System.out.println("thread2 is
running...");
};
Thread t2=new Thread(r2);
t2.start();
}

You might also like