Sie sind auf Seite 1von 11

//extends Thread

class MultithreadingDemo extends Thread{


public void run(){
System.out.println("My thread is in running state.");
}
public static void main(String args[]){
MultithreadingDemo obj=new MultithreadingDemo();
obj.start();
}
}

//Implements runnable interface

class MultiThreadRun implements Runnable{


public void run(){
System.out.println("My thread is in running state.");
}
public static void main(String args[]){
MultiThreadRun obj=new MultiThreadRun();
Thread tobj =new Thread(obj);
tobj.start();
}
}

//Multi threads for printing neg.,even and odd numbers

class ThreadX implements Runnable


{
public void run()
{
for(int i = 1;i<=5;i++)
{
System.out.println("ThreadX with i=" + -1*i);
}
System.out.println("Exiting Thread X");
}
}

class ThreadY implements Runnable


{
public void run()
{
for(int j = 1;j<=5;j++)
{
System.out.println("ThreadY with i=" + 2*j);
}
System.out.println("Exiting Thread Y");
}
}

class ThreadZ implements Runnable


{
public void run()
{
for(int k = 1;k<=5;k++)
{
System.out.println("ThreadZ with k=" + 2*(k-1));
}
System.out.println("Exiting Thread Z");
}
}
class MultiThreadRunnable
{
public static void main(String[] args)
{
ThreadX x = new ThreadX();
Thread t1 = new Thread(x);

ThreadY y = new ThreadY();


Thread t2 = new Thread(y);

ThreadZ z = new ThreadZ();


Thread t3 = new Thread(z);
//Thread t3 = new Thread(new Thread (z));

t1.start();
t2.start();
t3.start();
System.out.println("Multi-Threading is Over");

// getName() method

public class SSBThread1 implements Runnable


{
public static void main(String[] args) {
Thread ssbThread1 = new Thread("Hello Class");
Thread ssbThread2 = new Thread("Welcome");
ssbThread1.start();
ssbThread2.start();
System.out.println("Thread names are following:");
System.out.println(ssbThread1.getName());
System.out.println(ssbThread2.getName());
}
@Override
public void run() {

//Thread Priority
class A extend Thread
{
public void run()
{
System.out.println("Thread A started");
for(i=1;i<=4;i++)
{
System.out.println("From Thread A:i=" +i);
}
System.out.println("Exit from A");
}
}

class B extend Thread


{
public void run()
{
System.out.println("Thread B started");
for(j=1;j<=4;j++)
{
System.out.println("From Thread B:j=" +j);
}
System.out.println("Exit from B");
}
}

class C extend Thread


{
public void run()
{
System.out.println("Thread B started");
for(k=1;k<=4;k++)
{
System.out.println("From Thread C:k=" +k);
}
System.out.println("Exit from C");
}
}

class ThreadPriority
{
public static void main(String[] args)
{
A threadA = new A();
B threadB = new B();
C threadC = new C();

threadC.setPriority(Thread.MAX_PRIORITY);
threadB.setPriority(threadA.getPriority() + 1);
threadA.setPriority(Thread.MIN_PRIORITY);

System.out.println("Started Thread A");


threadA.start();
System.out.println("Started Thread B");
threadB.start();
System.out.println("Started Thread C");
threadC.start();
System.out.println("End of Main Thread ");
}
}

// run3 method

publicclassRunExp3extendsThread
{
publicvoidrun()
{
for(inti=1;i<6;i++)
{
try
{
Thread.sleep(500);
}catch(InterruptedExceptione){System.out.println(e);}
System.out.println(i);
}
}
publicstaticvoidmain(Stringargs[])
{
RunExp3t1=newRunExp3();
RunExp3t2=newRunExp3();
t1.run();
t2.run();
}
}

//isAlive() method

public class JavaIsAliveExp extends Thread


{
public void run()
{
try
{
Thread.sleep(300);
System.out.println("is run() method isAlive
"+Thread.currentThread().isAlive());
}
catch (InterruptedException ie) {
}
}
public static void main(String[] args)
{
JavaIsAliveExp t1 = new JavaIsAliveExp();
System.out.println("before starting thread isAlive: "+t1.isAlive());
t1.start();
System.out.println("after starting thread isAlive: "+t1.isAlive());
}
}
//without join()

public class oneThreadJoin extends Thread {


public void run()
{
System.out.println("Hello ");
try {
Thread.sleep(500);
}
catch (InterruptedException ie) {
}
System.out.println("World");
}
public static void main(String[] args)
{
oneThreadJoin c1 = new oneThreadJoin();
oneThreadJoin c2 = new oneThreadJoin();
c1.start();
c2.start();
}
}

//with join()

public class oneThreadWithJoin extends Thread {


public void run()
{
System.out.println("Hello ");
try {
Thread.sleep(300);
}
catch (InterruptedException ie) {
}
System.out.println("Smitha !");
}
public static void main(String[] args)
{
oneThreadWithJoin c1 = new oneThreadWithJoin();
oneThreadWithJoin c2 = new oneThreadWithJoin();
c1.start();

try {
c1.join(); // Waiting for c1 to finish
}
catch (InterruptedException ie) {
}

c2.start();
}
}

//Synchronized method

//example of java synchronized method

class Table{
synchronized void printTable(int n){//synchronized method
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
}

}
}

class MyThread1 extends Thread{


Table t;
MyThread1(Table t){
this.t=t;
}
public void run(){
t.printTable(5);
}

}
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
t.printTable(100);
}
}

public class TestSynchronization2{


public static void main(String args[]){
Table obj = new Table();//only one object
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
}
}

//Maximum priority

public class JavaSetPriorityExp1 extends Thread


{
public void run()
{
System.out.println("Priority of thread is:
"+Thread.currentThread().getPriority());
}
public static void main(String args[])
{
// creating one thread
JavaSetPriorityExp1 t1=new JavaSetPriorityExp1();
// print the maximum priority of this thread
t1.setPriority(Thread.MAX_PRIORITY);
// call the run() method
t1.start();
}
}

//Minimum priority

public class JavaSetPriorityExp2 extends Thread


{
public void run()
{
System.out.println("Priority of thread is:
"+Thread.currentThread().getPriority());
}
public static void main(String args[])
{
// creating one thread
JavaSetPriorityExp2 t1=new JavaSetPriorityExp2();
// print the minimum priority of this thread
t1.setPriority(Thread.MIN_PRIORITY);
// This will call the run() method
t1.start();
}
}

//Normal priority

public class JavaSetPriorityExp3 extends Thread


{
public void run()
{
System.out.println("Priority of thread is:
"+Thread.currentThread().getPriority());
}
public static void main(String args[])
{
// creating one thread
JavaSetPriorityExp3 t1=new JavaSetPriorityExp3();
// print the normal priority of this thread
t1.setPriority(Thread.NORM_PRIORITY);
// starting the thread
t1.start();
}
}

//setPriority and getPriority methods

public class JavaSetPriorityExp4 extends Thread


{
public void run()
{
System.out.println("running...");
}
public static void main(String args[])
{
// creating one thread
JavaSetPriorityExp4 t1=new JavaSetPriorityExp4();
JavaSetPriorityExp4 t2=new JavaSetPriorityExp4();
// set the priority
t1.setPriority(4);
t2.setPriority(7);
// print the user defined priority
System.out.println("Priority of thread t1 is: " + t1.getPriority()); //4
System.out.println("Priority of thread t2 is: " + t2.getPriority()); //7
// this will call the run() method
t1.start();
}
}

public class JavaSetPriorityExp5 extends Thread


{
public void run()
{
System.out.println("running...");
}
public static void main(String args[])
{
// creating one thread
JavaSetPriorityExp5 t1=new JavaSetPriorityExp5();
JavaSetPriorityExp5 t2=new JavaSetPriorityExp5();
// set the priority
t1.setPriority(12);
t2.setPriority(7);
// print exception because priority of t1 is greater than 10
System.out.println("Priority of thread t1 is: " + t1.getPriority());
System.out.println("Priority of thread t2 is: " + t2.getPriority());
// call the run() method
t1.start();
}
}
// Lab-3b

class Q {
int n;
boolean valueset = false;

synchronized int get() {


while (!valueset)
try {
wait();
} catch (InterruptedException e) {
System.out.println("Thread Interrupted");
}
System.out.println("Got :" + n);
valueset = false;
notify();
return n;
}

synchronized void put(int n) {


while (valueset)
try {
wait();
} catch (InterruptedException e) {
System.out.println("Thread interrupted");
}
this.n = n;
valueset = true;
System.out.println("put " + n);
notify();
}
}

class Producer implements Runnable {


Q q;

Producer(Q q) {
this.q = q;
new Thread(this, "Producer").start();
}

public void run() {


int i = 0;
while (true) {
q.put(i++);
}
}
}

class Consumer implements Runnable {


Q q;

Consumer(Q q) {
this.q = q;
new Thread(this, "Consumer").start();
}

public void run() {


int i = 0;
while (true) {
q.get();
}
}
}

class Thread3b {
public static void main(String args[]) {
Q q = new Q();
new Producer(q);
new Consumer(q);
System.out.println("press ctrl+c to exit");
}
}

//sleep() method

class SleepMethod extends Thread


{
public void run()
{
for(int i=1;i<5;i++)
{
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println(e);
}
System.out.println(i);
}
}

public static void main(String[] args)


{
SleepMethod t1=new SleepMethod();
//SleepMethod t2=new SleepMethod();
t1.start();
//t2.start();
}
}

//synchronized method

class Table{
synchronized void printTable(int n){//method synchronized
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
}

}
}

class MyThread1 extends Thread{


Table t;
MyThread1(Table t){
this.t=t;
}
public void run(){
t.printTable(5);
}

}
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
t.printTable(100);
}
}
class TestSynchronization1{
public static void main(String args[]){
Table obj = new Table();//only one object
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
}
}

Das könnte Ihnen auch gefallen