Krivalar Tutorials 
Krivalar Tutorials

Java Multithreading - yield method






Thread - yield() method

Example - Thread using yield() method


public class ThreadYieldExample extends Thread{
	String threadName;
	int number;
	public ThreadYieldExample(String name, int number){
		this.threadName=name;
		this.number=number;
	}
	public void run(){
		if(threadName.equals("Thread1")){
			System.out.println("Thread1 yielding..");
			yield();
		}
			System.out.println(threadName+" "+ Math.pow(number, 100));

	}

	public static void main(String a[]){
		ThreadYieldExample thread1= new ThreadYieldExample("Thread1",2);
		ThreadYieldExample thread2= new ThreadYieldExample("Thread2",3);
		ThreadYieldExample thread3= new ThreadYieldExample("Thread3",4);
		ThreadYieldExample thread4= new ThreadYieldExample("Thread4",5);

		thread1.start();
		thread2.start();
		thread3.start();
		thread4.start();
	}
}

Output - Thread yield() method

Thread1 yielding..
Thread4 7.888609052210118E69
Thread3 1.6069380442589903E60
Thread2 5.153775207320113E47
Thread1 1.2676506002282294E30