Skip to content

Commit a96f860

Browse files
committed
added demon, priority
1 parent 59e42b8 commit a96f860

File tree

6 files changed

+245
-4
lines changed

6 files changed

+245
-4
lines changed

.idea/workspace.xml

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ It is used to develop rich internet applications. It uses a light-weight user in
132132
|[Create Thread](https://github.com/connectaman/Java_Notes_and_Programs/blob/master/src/Multithreading/CreateThread.md)|
133133
|[Thread Sleep](https://github.com/connectaman/Java_Notes_and_Programs/blob/master/src/Multithreading/ThreadSleep.md)|
134134
|[run() Method](https://github.com/connectaman/Java_Notes_and_Programs/blob/master/src/Multithreading/RunMethod.md)|
135-
|[]()|
136-
|[]()|
135+
|[Join]()|
136+
|[Naming a thread]()|
137137
|[]()|
138138
|[]()|
139139
|[]()|

src/Multithreading/DemonThread.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
### Daemon Thread in Java
2+
3+
Daemon thread in java is a service provider thread that provides services to the user thread. Its life depend on the mercy of user threads i.e. when all the user threads dies, JVM terminates this thread automatically.
4+
5+
There are many java daemon threads running automatically e.g. gc, finalizer etc.
6+
7+
You can see all the detail by typing the jconsole in the command prompt. The jconsole tool provides information about the loaded classes, memory usage, running threads etc.
8+
9+
##### Points to remember for Daemon Thread in Java
10+
11+
12+
- It provides services to user threads for background supporting tasks. It has no role in life than to serve user threads.
13+
- Its life depends on user threads.
14+
- It is a low priority thread.
15+
16+
-------
17+
18+
##### Why JVM terminates the daemon thread if there is no user thread?
19+
20+
The sole purpose of the daemon thread is that it provides services to user thread for background supporting task. If there is no user thread, why should JVM keep running this thread. That is why JVM terminates the daemon thread if there is no user thread.
21+
22+
##### Methods for Java Daemon thread by Thread class
23+
24+
The java.lang.Thread class provides two methods for java daemon thread.
25+
26+
|No.| Method| Description|
27+
|-----|------|--------|
28+
|1) |public void setDaemon(boolean status) |is used to mark the current thread as daemon thread or user thread.|
29+
|2) |public boolean isDaemon() |is used to check that current is daemon.|
30+
31+
32+
---------
33+
34+
##### Simple example of Daemon thread in java
35+
36+
```java
37+
public class TestDaemonThread1 extends Thread{
38+
public void run(){
39+
if(Thread.currentThread().isDaemon()){//checking for daemon thread
40+
System.out.println("daemon thread work");
41+
}
42+
else{
43+
System.out.println("user thread work");
44+
}
45+
}
46+
public static void main(String[] args){
47+
TestDaemonThread1 t1=new TestDaemonThread1();//creating thread
48+
TestDaemonThread1 t2=new TestDaemonThread1();
49+
TestDaemonThread1 t3=new TestDaemonThread1();
50+
51+
t1.setDaemon(true);//now t1 is daemon thread
52+
53+
t1.start();//starting threads
54+
t2.start();
55+
t3.start();
56+
}
57+
}
58+
```
59+
Output
60+
```
61+
daemon thread work
62+
user thread work
63+
user thread work
64+
```
65+
66+
--------
67+
68+
##### Note: If you want to make a user thread as Daemon, it must not be started otherwise it will throw IllegalThreadStateException.
69+
70+
71+
```java
72+
class TestDaemonThread2 extends Thread{
73+
public void run(){
74+
System.out.println("Name: "+Thread.currentThread().getName());
75+
System.out.println("Daemon: "+Thread.currentThread().isDaemon());
76+
}
77+
78+
public static void main(String[] args){
79+
TestDaemonThread2 t1=new TestDaemonThread2();
80+
TestDaemonThread2 t2=new TestDaemonThread2();
81+
t1.start();
82+
t1.setDaemon(true);//will throw exception here
83+
t2.start();
84+
}
85+
}
86+
```
87+
```
88+
Output:exception in thread main: java.lang.IllegalThreadStateException
89+
```
90+
91+
92+
--------
93+

src/Multithreading/Join.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
### The join() method
2+
3+
The join() method waits for a thread to die. In other words, it causes the currently running threads to stop executing until the thread it joins with completes its task.
4+
5+
##### Syntax
6+
7+
public void join()throws InterruptedException
8+
public void join(long milliseconds)throws InterruptedException
9+
10+
```java
11+
class TestJoinMethod1 extends Thread{
12+
public void run(){
13+
for(int i=1;i<=5;i++){
14+
try{
15+
Thread.sleep(500);
16+
}catch(Exception e){System.out.println(e);}
17+
System.out.println(i);
18+
}
19+
}
20+
public static void main(String args[]){
21+
TestJoinMethod1 t1=new TestJoinMethod1();
22+
TestJoinMethod1 t2=new TestJoinMethod1();
23+
TestJoinMethod1 t3=new TestJoinMethod1();
24+
t1.start();
25+
try{
26+
t1.join();
27+
}catch(Exception e){System.out.println(e);}
28+
29+
t2.start();
30+
t3.start();
31+
}
32+
}
33+
```
34+
Output
35+
```
36+
1
37+
2
38+
3
39+
4
40+
5
41+
1
42+
1
43+
2
44+
2
45+
3
46+
3
47+
4
48+
4
49+
5
50+
5
51+
```
52+
53+
--------
54+

src/Multithreading/NamingThread.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
### Naming Thread and Current Thread
2+
3+
##### Naming Thread
4+
5+
The Thread class provides methods to change and get the name of a thread. By default, each thread has a name i.e. thread-0, thread-1 and so on. By we can change the name of the thread by using setName() method. The syntax of setName() and getName() methods are given below:
6+
7+
public String getName(): is used to return the name of a thread.
8+
public void setName(String name): is used to change the name of a thread.
9+
10+
11+
##### Example
12+
```java
13+
class TestMultiNaming1 extends Thread{
14+
public void run(){
15+
System.out.println("running...");
16+
}
17+
public static void main(String args[]){
18+
TestMultiNaming1 t1=new TestMultiNaming1();
19+
TestMultiNaming1 t2=new TestMultiNaming1();
20+
System.out.println("Name of t1:"+t1.getName());
21+
System.out.println("Name of t2:"+t2.getName());
22+
23+
t1.start();
24+
t2.start();
25+
26+
t1.setName("Hello");
27+
System.out.println("After changing name of t1:"+t1.getName());
28+
}
29+
}
30+
```
31+
32+
33+
--------
34+
Output
35+
```
36+
Output:Name of t1:Thread-0
37+
Name of t2:Thread-1
38+
id of t1:8
39+
running...
40+
After changeling name of t1:Hello
41+
running..
42+
```

src/Multithreading/Priority.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
### Priority of a Thread (Thread Priority):
2+
3+
Each thread have a priority. Priorities are represented by a number between 1 and 10. In most cases, thread schedular schedules the threads according to their priority (known as preemptive scheduling). But it is not guaranteed because it depends on JVM specification that which scheduling it chooses.
4+
5+
#### 3 constants defined in Thread class:
6+
7+
8+
1- public static int MIN_PRIORITY
9+
10+
2- public static int NORM_PRIORITY
11+
12+
3- public static int MAX_PRIORITY
13+
14+
15+
---------
16+
17+
##### Example of priority of a Thread:
18+
19+
```java
20+
class TestMultiPriority1 extends Thread{
21+
public void run(){
22+
System.out.println("running thread name is:"+Thread.currentThread().getName());
23+
System.out.println("running thread priority is:"+Thread.currentThread().getPriority());
24+
25+
}
26+
public static void main(String args[]){
27+
TestMultiPriority1 m1=new TestMultiPriority1();
28+
TestMultiPriority1 m2=new TestMultiPriority1();
29+
m1.setPriority(Thread.MIN_PRIORITY);
30+
m2.setPriority(Thread.MAX_PRIORITY);
31+
m1.start();
32+
m2.start();
33+
34+
}
35+
}
36+
```
37+
Output
38+
```
39+
running thread name is:Thread-0
40+
running thread priority is:10
41+
running thread name is:Thread-1
42+
running thread priority is:1
43+
```
44+
45+
46+
47+
48+

0 commit comments

Comments
 (0)