[go: up one dir, main page]

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

Thread

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 8

Example of naming a thread

1. class TestMultiNaming1 extends Thread{


2. public void run(){
3. System.out.println("running...");
4. }
5. public static void main(String args[]){
6. TestMultiNaming1 t1=new TestMultiNaming1();
7. TestMultiNaming1 t2=new TestMultiNaming1();
8. System.out.println("Name of t1:"+t1.getName());
9. System.out.println("Name of t2:"+t2.getName());
10.
11. t1.start();
12. t2.start();
13.
14. t1.setName("Sonoo Jaiswal");
15. System.out.println("After changing name of t1:"+t1.getName());
16. }
17. }
Test it Now

Output:Name of t1:Thread-0
Name of t2:Thread-1
id of t1:8
running...
After changeling name of t1:Sonoo Jaiswal
running...

Current Thread
The currentThread() method returns a reference of currently executing thread.

1. public static Thread currentThread()

Example of currentThread() method


1. class TestMultiNaming2 extends Thread{
2. public void run(){
3. System.out.println(Thread.currentThread().getName());
4. }
5. public static void main(String args[]){
6. TestMultiNaming2 t1=new TestMultiNaming2();
7. TestMultiNaming2 t2=new TestMultiNaming2();
8.
9. t1.start();
10. t2.start();
11. }
12. }
Test it Now

Output:Thread-0
Thread-1

Example of sleep method in java


1. class TestSleepMethod1 extends Thread{
2. public void run(){
3. for(int i=1;i<5;i++){
4. try{Thread.sleep(500);}catch(InterruptedException e){System.out.println(e);}
5. System.out.println(i);
6. }
7. }
8. public static void main(String args[]){
9. TestSleepMethod1 t1=new TestSleepMethod1();
10. TestSleepMethod1 t2=new TestSleepMethod1();
11.
12. t1.start();
13. t2.start();
14. }
15. }

Output:

1
1
2
2
3
3
4
4
Join Method
1. class TestJoinMethod1 extends Thread{
2. public void run(){
3. for(int i=1;i<=5;i++){
4. try{
5. Thread.sleep(500);
6. }catch(Exception e){System.out.println(e);}
7. System.out.println(i);
8. }
9. }
10. public static void main(String args[]){
11. TestJoinMethod1 t1=new TestJoinMethod1();
12. TestJoinMethod1 t2=new TestJoinMethod1();
13. TestJoinMethod1 t3=new TestJoinMethod1();
14. t1.start();
15. try{
16. t1.join();
17. }catch(Exception e){System.out.println(e);}
18.
19. t2.start();
20. t3.start();
21. }
22. }
Test it Now
Output:1
2
3
4
5
1
1
2
2
3
3
4
4
5
5
Synchronized Block :
class Counter {
void deposit()
{
synchronized(this) {
for(int i=1;i<5;i++) {
try{Thread.sleep(500);}catch(InterruptedException e)
{System.out.println(e);}
System.out.println(i);
}
} }

class TestSleepMethod2 extends Thread{


Counter t;
TestSleepMethod2(Counter t) {this.t=t; }
public void run(){
t.deposit();
}
public static void main(String args[]){
Counter obj = new Counter();
TestSleepMethod2 t1=new TestSleepMethod2(obj);
TestSleepMethod2 t2=new TestSleepMethod2(obj);

t1.start();
t2.start(); }}

}
}
Creating thread by implementing the runnable
interface
1. class NewThread implements Runnable {
2. String name;
3. Thread thread;
4. NewThread (String name){
5. this.name = name;
6. thread = new Thread(this, name);
7. System.out.println( "A New thread: " + thread+ "is created\n" );
8. thread.start();
9. }
10. public void run() {
11. try {
12. for(int j = 5; j > 0; j--) {
13. System.out.println(name + ": " + j);
14. Thread.sleep(1000);
15. }
16. }catch (InterruptedException e) {
17. System.out.println(name + " thread Interrupted");
18. }
19. System.out.println(name + " thread exiting.");
20. }
21.}
22.class ThreadExample2 {
23. public static void main(String args[]) {
24. new NewThread("1st");
25. new NewThread("2nd");
26. new NewThread("3rd");
27. try {
28. Thread.sleep(8000);
29. } catch (InterruptedException excetion) {
30. System.out.println("Inturruption occurs in Main Thread");
31. }
32. System.out.println("We are exiting from Main Thread");
33. }
34.}

Output:

In the above example, we perform the Multithreading by implementing the runnable


interface. To learn more about Multithreading, click here.

Class SampleThread implement runnable


{ void run()
{ System.out.println(“ I am in SampleThread”);
}
}
Class MainThread
{ public static void main(String args[])
{
SampleThread t1=new SampleThread();

Thread mythread=new Thread(t1);

Mythread.start();

You might also like