first commit
This commit is contained in:
BIN
Multi.class
Normal file
BIN
Multi.class
Normal file
Binary file not shown.
10
Multi.java
Normal file
10
Multi.java
Normal file
@@ -0,0 +1,10 @@
|
||||
// write a java code by extending thread class
|
||||
class Multi extends Thread{
|
||||
public void run(){
|
||||
System.out.println("Thread is running");
|
||||
}
|
||||
public static void main(String args[]){
|
||||
Multi t1 = new Multi();
|
||||
t1.start();
|
||||
}
|
||||
}
|
||||
BIN
Multi3.class
Normal file
BIN
Multi3.class
Normal file
Binary file not shown.
12
Multi3.java
Normal file
12
Multi3.java
Normal file
@@ -0,0 +1,12 @@
|
||||
// write a java code by implementing runnable interface.
|
||||
|
||||
class Multi3 implements Runnable{
|
||||
public void run(){
|
||||
System.out.println("thread is running...");
|
||||
}
|
||||
public static void main(String args[]){
|
||||
Multi3 m1 = new Multi3();
|
||||
Thread t1 = new Thread(m1);
|
||||
t1.start();
|
||||
}
|
||||
}
|
||||
33
MultipleThread.java
Normal file
33
MultipleThread.java
Normal file
@@ -0,0 +1,33 @@
|
||||
// write a java program for multiple thread acting on a single object
|
||||
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(1000);
|
||||
}catch(InterruptedException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
public static void main(String args[]){
|
||||
Thread nThread = Thread.currentThread();
|
||||
System.out.println("Name of the Thread: " + nThread);
|
||||
MultipleThread mt = new MultipleThread("Hello Java");
|
||||
Thread t1 = new Thread(mt);
|
||||
Thread t2 = new Thread(mt);
|
||||
Thread t3 = new Thread(mt);
|
||||
t1.start();
|
||||
System.out.println("thread" + t1 );
|
||||
t2.start();
|
||||
System.out.println("thread " + t2);
|
||||
t3.start();
|
||||
|
||||
int count = Thread.activeCount();
|
||||
System.out.println("No of active threads: " + count);
|
||||
}
|
||||
}
|
||||
25
MyThread.java
Normal file
25
MyThread.java
Normal file
@@ -0,0 +1,25 @@
|
||||
// write a java code for two thread performing two task at a time
|
||||
public class MyThread extends Thread{
|
||||
String task;
|
||||
MyThread(String task){
|
||||
this.task = task;
|
||||
}
|
||||
public void run(){
|
||||
synchronized(this) {
|
||||
for(int i=1;i<=5;i++){
|
||||
System.out.println(task + ":" + i);
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
}catch(InterruptedException ie){
|
||||
System.out.println(ie.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public static void main(String args[]){
|
||||
MyThread th1 = new MyThread("Cut the ticket");
|
||||
MyThread th2 = new MyThread("Show your seat number");
|
||||
th1.start();
|
||||
th2.start();
|
||||
}
|
||||
}
|
||||
BIN
RunnableTest.class
Normal file
BIN
RunnableTest.class
Normal file
Binary file not shown.
19
RunnableTest.java
Normal file
19
RunnableTest.java
Normal file
@@ -0,0 +1,19 @@
|
||||
class x implements Runnable{
|
||||
public void run(){
|
||||
for(int i=0;i<=5;i++){
|
||||
System.out.println("The Thread x is: " + i);
|
||||
}
|
||||
|
||||
System.out.println("End of the Thread x");
|
||||
}
|
||||
}
|
||||
|
||||
class RunnableTest{
|
||||
public static void main(String args[]){
|
||||
x r = new x();
|
||||
Thread threadx = new Thread(r);
|
||||
threadx.start();
|
||||
System.out.println("The End of the main thread");
|
||||
}
|
||||
}
|
||||
|
||||
15
TestMultiPriority1.java
Normal file
15
TestMultiPriority1.java
Normal file
@@ -0,0 +1,15 @@
|
||||
// write a java code for demonstrating thread priority
|
||||
class TestMultiPriority1 extends Thread{
|
||||
public void run(){
|
||||
System.out.println("running thread name is: " + Thread.currentThread().getName());
|
||||
System.out.println("running thread priority is: " + Thread.currentThread().getPriority());
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
TestMultiPriority1 m1 = new TestMultiPriority1();
|
||||
TestMultiPriority1 m2 = new TestMultiPriority1();
|
||||
m1.setPriority(2);
|
||||
m2.setPriority(Thread.MAX_PRIORITY);
|
||||
m1.start();
|
||||
m2.start();
|
||||
}
|
||||
}
|
||||
BIN
ThreadExample1.class
Normal file
BIN
ThreadExample1.class
Normal file
Binary file not shown.
13
ThreadExample1.java
Normal file
13
ThreadExample1.java
Normal file
@@ -0,0 +1,13 @@
|
||||
public class ThreadExample1 extends Thread{
|
||||
public void run(){
|
||||
int a = 0;
|
||||
int b = 12;
|
||||
int result = a + b;
|
||||
System.out.println("Thread is running");
|
||||
System.out.println("Sum of two numbers is: " + result);
|
||||
}
|
||||
public static void main(String args[]){
|
||||
ThreadExample1 t1 = new ThreadExample1();
|
||||
t1.start();
|
||||
}
|
||||
}
|
||||
10
p1.java
Normal file
10
p1.java
Normal file
@@ -0,0 +1,10 @@
|
||||
// write a java code by extending thread class
|
||||
class Multi extends Thread{
|
||||
public void run(){
|
||||
System.out.println("Thread is running");
|
||||
}
|
||||
public static void main(String args[]){
|
||||
Multi t1 = new Multi();
|
||||
t1.start();'
|
||||
}
|
||||
}
|
||||
BIN
x$RunnableTest.class
Normal file
BIN
x$RunnableTest.class
Normal file
Binary file not shown.
Reference in New Issue
Block a user