Friday, August 4, 2023

Thread class in Java - 6

 6. Priority

class ThreadPrio implements Runnable

{

Thread t;

ThreadPrio()

{

t=new Thread(this,"hello thread");

System.out.println(t);

t.start();

}

public void run()

{

System.out.println("hello thread started---");

try

{

Thread.sleep(1000);

}

catch(InterruptedException e)

{

}

System.out.println(t);

}

}

class T6Demo

{

public static void main(String args[])


{

ThreadPrio ob1=new ThreadPrio();

System.out.println("main thread "+Thread.currentThread());

ob1.t.setPriority(4);

}

}

o/p:------------------------------------------------------------------------------------------

ddmc@ddmc-desktop:~$ java T6Demo

Thread[hello thread,5,main]

hello thread started---

main thread Thread[main,5,main]

Thread[hello thread,4,main]

Thread class in Java - 5

 5. isAlive() and join()

class CreateOne implements Runnable

{

String name;

Thread t;

CreateOne(String s)

{

name=s;

t=new Thread(this,name);

System.out.println("First thread "+t);

t.start();

}

public void run()

{

try

{

for(int i=0;i<5;i++)

{

System.out.println(name+" : "+i);

Thread.sleep(1000);

}

}

catch(InterruptedException e)

{

System.out.println("caught");

}

System.out.println(name + "exiting");

}

}

class CreateTwo implements Runnable

{

String name;


Thread t;

CreateTwo(String s)

{

name=s;

t=new Thread(this,name);

System.out.println("First thread "+t);

t.start();

}

public void run()

{

try

{

for(int i=0;i<5;i++)

{

System.out.println(name+" : "+i);

Thread.sleep(500);

}

}

catch(InterruptedException e)

{

System.out.println("caught");

}

System.out.println(name + "exiting");

}

}

class T5Demo

{

public static void main(String args[])

{

CreateOne ob1=new CreateOne("One");

CreateTwo ob2=new CreateTwo("Two");

System.out.println("First thread is alive or not ---"+ob1.t.isAlive());

System.out.println("Second thread is alive or not ---"+ob2.t.isAlive());

//wait for the threads to finish

try

{

ob1.t.join();

ob2.t.join();

}

catch(InterruptedException e)

{

}

System.out.println("First thread is alive or not ---"+ob1.t.isAlive());

System.out.println("Second thread is alive or not ---"+ob2.t.isAlive());

System.out.println("exiting main");

}

}


o/p:-------------------------------------------------------------

ddmc@ddmc-desktop:~$ java T5Demo

First thread Thread[One,5,main]

First thread Thread[Two,5,main]

First thread is alive or not ---true

Second thread is alive or not ---true

One : 0

Two : 0

Two : 1

One : 1

Two : 2

Two : 3

One : 2

Two : 4

Twoexiting

One : 3

One : 4

Oneexiting

First thread is alive or not ---false

Second thread is alive or not ---false

exiting main

Thread class in Java - 4

 4. Creating multiple threads

class Multi implements Runnable

{

String name;

Thread t;

Multi(String s)

{

name=s;

t=new Thread(this,name);

System.out.println("new thread "+t);

t.start();

}

public void run()

{

try


{

for(int i=5;i>0;i--)

{

System.out.println(name+" : "+i);

Thread.sleep(1000);

}

}

catch(InterruptedException e)

{

System.out.println(name + "interrupted");

}

System.out.println(name + "exiting");

}

}

class T4Demo

{

public static void main(String args[])

{

new Multi("one");

new Multi("two");

new Multi("three");

try

{

//wait for the other thread

Thread.sleep(10000);

}

catch(InterruptedException e)

{

System.out.println("main thread interrupted ");

}

System.out.println("exiting main thread ");

}

}

o/p:------------------------------------------------------------------------------


class Multi implements Runnable

{

String name;

Thread t;

Multi(String s)

{

name=s;

t=new Thread(this,name);

System.out.println("new thread "+t);

t.start();


}

public void run()

{

try

{

for(int i=5;i>0;i--)

{

System.out.println(name+" : "+i);

Thread.sleep(1000);

}

}

catch(InterruptedException e)

{

System.out.println(name + "interrupted");

}

System.out.println(name + "exiting");

}

}

class T4Demo

{

public static void main(String args[])

{

new Multi("one");

new Multi("two");

new Multi("three");

try

{

//wait for the other thread

Thread.sleep(10000);

}

catch(InterruptedException e)

{

System.out.println("main thread interrupted ");

}

System.out.println("exiting main thread ");

}

}

o/p: ---------------- -----------------------------------

ddmc@ddmc-desktop:~$ java T4Demo

new thread Thread[one,5,main]

new thread Thread[two,5,main]

new thread Thread[three,5,main]

one : 5

two : 5

three : 5

one : 4


three : 4

two : 4

one : 3

three : 3

two : 3

one : 2

three : 2

two : 2

one : 1

three : 1

two : 1

threeexiting

twoexiting

oneexiting

exiting main thread

Thread class in Java - 3

 3. Creating thread by extending Thread class

class MyNewThread extends Thread

{

MyNewThread()

{

//creating new thread

super("Demo thread");

System.out.println("child thread "+this);

start();

}

public void run()

{

try

{

for(int i=0;i<5;i++)

{

System.out.println("child thread "+i);

Thread.sleep(500);

}

}

catch(InterruptedException e)

{

System.out.println("child interrupted ");

}

System.out.println("exiting child thread----");

}

}

class T3Demo

{

public static void main(String args[])

{


new MyNewThread();

try

{

for(int i=90;i<95;i++)

{

System.out.println("main thread "+i);

Thread.sleep(1000);

}

}

catch(InterruptedException e)

{

System.out.println("main interrupted ");

}

System.out.println("exiting main thread----");

}

}

o/p:--------------------------------------------------------------------------------------------------

ddmc@ddmc-desktop:~$ java T3Demo

child thread Thread[Demo thread,5,main]

child thread 0

main thread 90

child thread 1

child thread 2

main thread 91

child thread 3

child thread 4

main thread 92

exiting child thread----

main thread 93

main thread 94

exiting main thread----

Thread class in Java - 1

 Java Thread Programs

1. Dealing with main

class T1Demo

{

public static void main(String args[])

{

Thread t=Thread.currentThread();

System.out.println("current "+t);

System.out.println(t.getName());

t.setName("MyMain");

System.out.println("current now "+t);

System.out.println(t.getName());

try

{

for(int i=0;i<10;i++)

{

System.out.println(i);

Thread.sleep(1000);

}

}

catch(InterruptedException e)

{

System.out.println("caught");

}

}

}

o/p: --------------------------------------------------------------------------------------------

ddmc@ddmc-desktop:~$ java T1Demo

current Thread[main,5,main]

main

current now Thread[MyMain,5,main]

MyMain

0

1

2

3

4

5

6

7

8

9

In [main, 5, main], 5 is the priority of the thread, the last main is the group name of the thread.

Thread class in Java - 2

 2. Creating new thread by implementing Runnable

class NewThread implements Runnable

{

Thread t;

NewThread()

{

//create a new thread

t=new Thread(this,"Demo Thread");

System.out.println("child thread "+t);

t.start();

}

public void run()

{

try

{

for(int i=5;i>0;i--)

{

System.out.println("child "+i);

Thread.sleep(500);

}

}

catch(InterruptedException e)

{

System.out.println("child interrupted");

}

System.out.println("exiting child thread");

}

}

class T2Demo

{

public static void main(String args[])

{

new NewThread();

try

{

for(int i=100;i>95;i--)

{

System.out.println("Main "+i);

Thread.sleep(1000);

}

}

catch(InterruptedException e)

{

System.out.println("main interrupted");

}

System.out.println("exiting main thread");

}

}


o/p: -------------------------------------------------------------------------------------------

ddmc@ddmc-desktop:~$ java T2Demo

child thread Thread[Demo Thread,5,main]

child 5

Main 100

child 4

Main 99

child 3

child 2

Main 98

child 1

exiting child thread

Main 97

Main 96

exiting main thread

--------------------------------------------------------------------------------------------------

A physicist confesses that the electron is round and that Jesus rose from the dead. (Bengali)

একজন পদার্থবিজ্ঞানী স্বীকার করেন যে ইলেকট্রন গোলাকার এবং যিশু মৃতদের মধ্য থেকে পুনরুত্থিত হয়েছিলেন। আমার নিজের বিশ্বাস প্রমাণ দ্বারা প্রভাব...