Last active
December 2, 2019 22:24
-
-
Save AnujJha-stack/58f721e63f459356edac4466e8e9b690 to your computer and use it in GitHub Desktop.
Java_Thread
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static java.lang.Thread.sleep; | |
class Player implements Runnable{ | |
private int speed; | |
private String name; | |
private static int coveredDistance=0; | |
Player(String name, int speed){ | |
this.name = name; | |
this.speed = speed; | |
} | |
public void run(){ | |
System.out.println("I am "+name+"with speed"+speed); | |
while(coveredDistance < 1000){ | |
if(name.equals("rabbit")){ | |
coveredDistance += speed; | |
if(coveredDistance >= 1000){ | |
System.out.println(name+" WINS"); | |
} | |
} | |
if(name.equals("turtle")){ | |
coveredDistance += speed; | |
if(coveredDistance >= 1000){ | |
System.out.println(name+" WINS"); | |
} | |
} | |
// rabbit sleeps at 900m | |
if(name.equals("rabbit") && coveredDistance >= 900){ | |
try{ | |
sleep(999); | |
} | |
catch (InterruptedException e){ | |
System.out.println("Exception : "+e); | |
} | |
finally{ | |
coveredDistance += speed; | |
} | |
} | |
} | |
} | |
} | |
public class race { | |
public static void main(String[] args){ | |
Thread t1 = new Thread(new Player("turtle",1)); | |
Thread t2 = new Thread(new Player("rabbit",10)); | |
t1.start(); | |
t2.start(); | |
} | |
} | |
//>>>>>turtle wins. | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//synchronized block | |
import java.util.Scanner; | |
class Account{ | |
private int balance; | |
Account(int balance){ | |
this.balance = balance; | |
} | |
private boolean isSufficient(int balance){ | |
return (this.balance >= balance); | |
} | |
public void withdraw(int balance,String name){ | |
if(isSufficient(balance)){ | |
this.balance -= balance; | |
System.out.println(name+" Your Withdrawal money : "+balance); | |
System.out.println("Available Balance : "+this.balance); | |
} | |
else{ | |
System.out.println(name+" Your Withdrawal money : "+balance); | |
System.out.println("Insufficient Fund !!! \nAvailable balance : "+this.balance); | |
} | |
} | |
} | |
class Customer implements Runnable{ | |
private Account account; | |
private String name; | |
public Customer(String name,Account account){ | |
this.account = account; | |
this.name = name; | |
} | |
public void run(){ | |
synchronized (account){ | |
Scanner input = new Scanner(System.in); | |
System.out.println(name+" Enter amount to withdraw :"); | |
int amount = input.nextInt(); | |
account.withdraw(amount,name); | |
} | |
} | |
} | |
public class ThreadSynchronizedBlock { | |
public static void main(String[] args){ | |
Account a1 = new Account(1000); | |
Customer c1 = new Customer("Raj",a1), c2 = new Customer("Simran",a1); | |
Thread t1 = new Thread(c1); | |
Thread t2 = new Thread(c2); | |
t1.start(); | |
t2.start(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//synchronized method. | |
import java.util.Scanner; | |
class Account{ | |
private int balance; | |
Account(int balance){ | |
this.balance = balance; | |
} | |
private boolean isSufficient(int balance){ | |
return (this.balance >= balance); | |
} | |
public synchronized void withdraw(int balance,String name){ | |
if(isSufficient(balance)){ | |
this.balance -= balance; | |
System.out.println(name+" Your Withdrawal money : "+balance); | |
System.out.println("Available Balance : "+this.balance); | |
} | |
else{ | |
System.out.println(name+" Your Withdrawal money : "+balance); | |
System.out.println("Insufficient Fund !!! \nAvailable balance : "+this.balance); | |
} | |
} | |
} | |
class Customer implements Runnable{ | |
private Account account; | |
private String name; | |
public Customer(String name,Account account){ | |
this.account = account; | |
this.name = name; | |
} | |
public void run(){ | |
Scanner input = new Scanner(System.in); | |
System.out.println(name+" Enter amount to withdraw :"); | |
int amount = input.nextInt(); | |
account.withdraw(amount,name); | |
} | |
} | |
public class ThreadSynchronizedMethod { | |
public static void main(String[] args){ | |
Account a1 = new Account(1000); | |
Customer c1 = new Customer("Raj",a1), c2 = new Customer("Simran",a1); | |
Thread t1 = new Thread(c1); | |
Thread t2 = new Thread(c2); | |
t1.start(); | |
t2.start(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A implements Runnable{ | |
public void run() { | |
for (int i = 1; i <= 10; i++){ | |
System.out.println("Thread A "+i); | |
} | |
} | |
} | |
class B implements Runnable{ | |
public void run() { | |
for (int i = 1; i <= 10; i++){ | |
System.out.println("Thread B "+i); | |
} | |
} | |
} | |
public class ThreadUsingRunnable { | |
public static void main(String[] args){ | |
Thread T1 = new Thread(new A()); | |
Thread T2 = new Thread(new B()); | |
T1.start();//T1 start Execution | |
T2.start();//T2 starts Executions. | |
//there will be random execution | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A extends Thread{ | |
public void run() { | |
for (int i = 1; i <= 10; i++){ | |
System.out.println("Thread A "+i); | |
} | |
} | |
} | |
class B extends Thread{ | |
public void run() { | |
for (int i = 1; i <= 10; i++){ | |
System.out.println("Thread B "+i); | |
} | |
} | |
} | |
public class Java_ThreadUsingThreadClass{ | |
public static void main(String[] args){ | |
A T1 = new A(); | |
B T2 = new B(); | |
T1.start();//T1 start Execution | |
T2.start();//T2 starts Executions. | |
//there will be random execution | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment