Added 5th and 6th program
This commit is contained in:
BIN
MultipleThread.class
Normal file
BIN
MultipleThread.class
Normal file
Binary file not shown.
30
MultipleThread.java
Normal file
30
MultipleThread.java
Normal file
@@ -0,0 +1,30 @@
|
||||
public class MultipleThread implements Runnable {
|
||||
String task;
|
||||
MultipleThread(String task){
|
||||
this.task = task;
|
||||
}
|
||||
public void run(){
|
||||
for(int i = 0; i<= 5; i++){
|
||||
System.out.println(task+ ":" + i);
|
||||
try {
|
||||
Thread.sleep(400);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
public static void main(String[] args){
|
||||
Thread nThread = Thread.currentThread();
|
||||
System.out.println("Name of thread : "+ nThread);
|
||||
|
||||
MultipleThread mt = new MultipleThread("hello");
|
||||
Thread t1 = new Thread(mt);
|
||||
Thread t2 = new Thread(mt);
|
||||
Thread t3 = new Thread(mt);
|
||||
t1.start();
|
||||
t2.start();
|
||||
t3.start();
|
||||
int count = Thread.activeCount();
|
||||
System.out.println("No. of active threads: " +count);
|
||||
}
|
||||
}
|
||||
BIN
Sync6.class
Normal file
BIN
Sync6.class
Normal file
Binary file not shown.
43
Sync6.java
Normal file
43
Sync6.java
Normal file
@@ -0,0 +1,43 @@
|
||||
// 6. write a java program for demonstrating Thread Synchronization
|
||||
class Table {
|
||||
synchronized void printTable(int n){
|
||||
for(int i = 1; i<= 5; i++){
|
||||
System.out.println(n * i);
|
||||
try {
|
||||
Thread.sleep(200);
|
||||
} catch (Exception e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Thread1 extends Thread {
|
||||
Table t;
|
||||
Thread1 (Table t){
|
||||
this.t = t;
|
||||
}
|
||||
public void run(){
|
||||
t.printTable(5);
|
||||
}
|
||||
}
|
||||
|
||||
class Thread2 extends Thread {
|
||||
Table t;
|
||||
Thread2(Table t){
|
||||
this.t = t;
|
||||
}
|
||||
public void run(){
|
||||
t.printTable(100);
|
||||
}
|
||||
}
|
||||
|
||||
public class Sync6 {
|
||||
public static void main(String args[]){
|
||||
Table obj = new Table();
|
||||
Thread1 t1 = new Thread1(obj);
|
||||
Thread2 t2 = new Thread2(obj);
|
||||
t1.start();
|
||||
t2.start();
|
||||
}
|
||||
}
|
||||
BIN
Table.class
Normal file
BIN
Table.class
Normal file
Binary file not shown.
BIN
Thread1.class
Normal file
BIN
Thread1.class
Normal file
Binary file not shown.
BIN
Thread2.class
Normal file
BIN
Thread2.class
Normal file
Binary file not shown.
BIN
src/6Sync.png
Normal file
BIN
src/6Sync.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 9.9 KiB |
@@ -1,4 +1,5 @@
|
||||
1. write a java code by extending thread class (2)
|
||||
2. write a java code by implementing runnable interface. (2)
|
||||
3. write a java code for demonstrating thread priority
|
||||
4. write a java code for two threads performing two tasks at a time
|
||||
4. write a java code for two threads performing two tasks at a time
|
||||
5. write a java program for multiple thread acting on single object
|
||||
Reference in New Issue
Block a user