00:00
/* 01- create thread using thread class */
class Test extends Thread{ //1
public void run(){ //2
System.out.println("using thread class");
} }
public class N02 {
public static void main(String[] args) {
Test t = new Test(); //3
t.start(); //4
} }
/* 02- Create thread using runnable interface */
class Test implements Runnable{ //1
public void run() { //2
System.out.println("using runnable interface");
} }
public class N02 {
public static void main(String[] args) {
Test t = new Test(); // 3
Thread th = new Thread(t); // ct call, specifically Test's clS run m/t
th.start(); //5
/* cant directly start the start m/T */
t.start(); // runnable doenot have start m/T
} }
/* 03- same thread start twice + multiple thread same task */
class N02 {
public static void main(String[] args) {
Test t = new Test();
/* invoke same thread twice */
t.start();
t.start(); //java.lang.IllegalThreadStateException
/* same task , many thread */
A obj1 = new A();
obj1.start();
B obj2 = new B();
obj2.start();
} }
class A extends Thread {
public void run() {
System.out.println("cant start dead thread");
} }
class B extends Thread {
public void run() {
System.out.println("single task by many thread");
} }
/* 04- st, get, current thread, alive*/
class N02 {
public static void main(String[] args) {
/* main thread */
System.out.println("This Thread is = "+Thread.currentThread().getName());//getname
/* set current thread name */
Thread.currentThread().setName("rajat");// setname
/* print main thread */
System.out.println(Thread.currentThread().getName());
/* give exception with the thD name = rajat */
int x = 100/0;
Test t1 = new Test();
Thread td = new Thread(t1);
/* rename the thead-0*/
td.setName("book");//setname
td.start();
/* is alive current thread */
boolean b1 = Thread.currentThread().isAlive();//isAlive
if(b1){
System.out.println("main thread is alive");
}else{
System.out.println("main thread is dead");
}
/* is alive thread-0 */
boolean b2 = td.isAlive();
if(b2){
System.out.println("td thread is alive");
}else{
System.out.println("td thread is dead");
} } }
class Test implements Runnable{
public void run(){
/* this is thread 0 */
System.out.println("This Thread is = "+Thread.currentThread().getName()); //getName = thread 0
/* rename the thread within body */
Thread.currentThread().setName("cap");
System.out.println("This Thread is = "+Thread.currentThread().getName());
} }
/* 05- damenon, set, is */
public class N02 extends Thread{
public void run() {
System.out.println(Thread.currentThread().getName());
System.out.println(Thread.currentThread().isDaemon());// isdamon = true
}
/* No Task : nothing start */
public static void main(String[] args) {
N02 t1 = new N02();
t1.setDaemon(true);// setdemomn(true)
t1.start();
}
/* With task */
public void book(){
N02 t2 = new N02();
System.out.println("main with task");
t2.setName("other method");// no effect
t2.setDaemon(true);
t2.start();
}
/* set dameon after start */
public void copy(){
N02 t2 = new N02();
System.out.println("main with task");
t2.start();
t2.setDaemon(true); //ot effect
} }
/* 06- get, set priority */
public class N02 extends Thread {
public void run() {
System.out.println("other thD priority : "+Thread.currentThread().getPriority());
}
public static void main(String[] args) {
System.out.println("main thD priority : "+Thread.currentThread().getPriority());
N02 t1 = new N02();
t1.setPriority(4);//setPriority
t1.start();
} }
/* 07- sleep with thread + with wrong parameters*/
public class N02 extends Thread{
public void run(){
for(int i=0;i<=10;i++){
try{
Thread.sleep(1000);
/*
with wrong parameters*/
Thread.sleep(1000,9999999999); //error
System.out.print(i+" " );
}
catch(Exception e){
e.getMessage();
} } }
public static void main(String[] args) {
System.out.println("Main thD");
/*
simple pirnt 1 - 10 after oen sec */
N02 t1 = new N02();
t1.start();
/*
with multiple thread invoke, op not fixed */
N02 t2 = new N02();
t2.start();
N02 t3 = new N02();
t3.start();
/*
using run mt, behave like a normal mt, firt execiute t4 then t5 */
N02 t4 = new N02();
t4.run();
N02 t5 = new N02();
t5.run();
} }
/* 08- yield code */
public class N02 extends Thread{
public void run(){
for(int i=0;i<=5;i++){
System.out.println(i+":"+Thread.currentThread().getName()+",");
} }
/*
simple mt w/o yield, op not fixed */
public static void main(String[] args) {
System.out.println("Main thD");
N02 t1 = new N02();
t1.start();
for(int i=0;i<=5;i++){
System.out.println(i+":"+Thread.currentThread().getName()+",");
} }
/*
mt with yield , op not fixed in windos */
public static void main(String[] args) {
System.out.println("yield in main m/t");
Thread.yield();
N02 t1 = new N02();
t1.start();
for(int i=0;i<=5;i++){
System.out.println(i+":"+Thread.currentThread().getName()+",");
} } }
/* 09- yield() m/t in child, op not fixed */
public class N02 extends Thread{
public void run(){
Thread.yield();
for(int i=0;i<=5;i++){
System.out.println(i+":"+Thread.currentThread().getName()+",");
} }
public static void main(String[] args) {
System.out.println("yield in child class");
N02 t1 = new N02();
t1.start();
for(int i=0;i<=5;i++){
System.out.println(i+":"+Thread.currentThread().getName()+",");
} } }
/* 10- join code : sleep code in both mt not fixed */
public class N02 extends Thread{
public void run(){
try{
for(int i=1;i<=5;i++){
System.out.println("child thread = "+i);
sleep(1000);
} }
catch(Exception e1){ e1.getMessage(); }
}
public static void main(String[] args) {
N02 t = new N02();
t.start();
try{
for(int i=1;i<=5;i++){
System.out.println("main thread = "+i);
Thread.sleep(1000);
} }
catch(Exception e){ e.printStackTrace(); }
} }
/* 11- join m/t */
public class N02 extends Thread{
public void run(){
try{
for(int i=1;i<=5;i++){
System.out.println("child thread = "+i);
sleep(1000);
} }
catch(Exception e1){ e1.getMessage(); }
}
public static void main(String[] args) throws InterruptedException{
N02 t = new N02();
t.start();
t.join(); // Current thD ruk TO JOIN AFTER child thD
try{
for(int i=1;i<=5;i++){
System.out.println("main thread = "+i);
Thread.sleep(1000);
} }
catch(Exception e){ e.printStackTrace(); }
} }
/* 12- main thread join after clD thD */
public class N02 extends Thread {
static Thread mainThread;// make instance
public void run(){
try{
mainThread.join(); // main thD ruk TO JOIN AFTER main thD
for(int i=1;i<=5;i++){
System.out.println("child thread = "+i);
sleep(1000);
} }
catch(Exception e1){ e1.getMessage(); }
}
public static void main(String[] args) throws InterruptedException{
mainThread = Thread.currentThread();
N02 t = new N02();
t.start();
try{
for(int i=1;i<=5;i++){
System.out.println("main thread = "+i);
Thread.sleep(1000);
} }
catch(Exception e){ e.printStackTrace(); }
} }
/* 13 - intrupt m/t and theory , start print 1 , as goes to sleep, intrupt,stop*/
public class N02 extends Thread {
public void run (){
try{
for(int i=1;i<=5;i++){
System.out.println(i);
Thread.sleep(1000);
} }
catch(Exception e) { e.printStackTrace(); }
}
public static void main(String[] args) {
N02 t1 = new N02();
t1.start();
t1.interrupt();
} }
/* 14 - intrupt m/t w/o sleep m/t , no effect at all */
public class N02 extends Thread {
public void run (){
try{
for(int i=1;i<=5;i++){
System.out.println(i);
// Thread.sleep(1000); no effect
} }
catch(Exception e) { e.printStackTrace(); }
}
public static void main(String[] args) {
N02 t1 = new N02();
t1.start();
t1.interrupt();
} }
/* 15 - interrupted code ,True then print number */
public class N02 extends Thread {
public void run (){
boolean x = Thread.interrupted(); // static m/t
System.out.println(x);
try{
for(int i=1;i<=5;i++){
System.out.println(i);
Thread.sleep(500);
} }
catch(Exception e) { e.printStackTrace(); }
}
public static void main(String[] args) {
N02 t1 = new N02();
t1.start();
t1.interrupt();
} }
/* 16 - isInterrupted code ,print true then 1 then stop */
public class N02 extends Thread {
public void run (){
boolean x = Thread.currentThread().isInterrupted(); // non static m/t
System.out.println(x);
try{
for(int i=1;i<=5;i++){
System.out.println(i);
Thread.sleep(500);
} }
catch(Exception e) { e.printStackTrace(); }
}
public static void main(String[] args) {
N02 t1 = new N02();
t1.start();
t1.interrupt();
} }