Friday, August 4, 2023

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.

No comments:

Post a Comment

Complete Works of Swami Vivekananda [Volume 8,Page - 2069]

  Complete Works of Swami Vivekananda [ Volume 8, Page - 2069] Jesus Christ was God — the Personal God become man. He has manifested Himsel...