class BitwiseOperators2 {
public static void main(String args[]) {
short s = 0xff;
System.out.println(s);
System.out.println(s & 0xf);
System.out.println(s | 1);
System.out.println(s ^ 1);
System.out.println(~s);
System.out.println(s >> 2);
System.out.println(s >>> 2);
System.out.println(s << 2);
}
}
Deadlock Demo in java
class A
{
B b;
synchronized void a1()
{
System.out.println("Starting a1");
b.b2();
}
synchronized void a2()
{
System.out.println("Starting a2");
}
}
class B
{
A a;
synchronized void b1() {
System.out.println("Starting b1");
a.a2();
}
synchronized void b2() {
System.out.println("Starting b2");
}
}
class Thread1 extends Thread {
A a;
Thread1(A a) {
this.a = a;
}
public void run() {
for(int i = 0; i <>
a.a1();
}
}
class Thread2 extends Thread {
B b;
Thread2(B b) {
this.b = b;
}
public void run() {
for(int i = 0; i <>
b.b1();
}
}
class DeadlockDemo {
public static void main(String args[]) {
// Create objects
A a = new A();
B b = new B();
a.b = b;
b.a = a;
// Create threads
Thread1 t1 = new Thread1(a);
Thread2 t2 = new Thread2(b);
t1.start();
t2.start();
// Wait for threads to complete
try {
t1.join();
t2.join();
}
catch(Exception e) {
e.printStackTrace();
}
// Display message
System.out.println("Done!");
}
}
{
B b;
synchronized void a1()
{
System.out.println("Starting a1");
b.b2();
}
synchronized void a2()
{
System.out.println("Starting a2");
}
}
class B
{
A a;
synchronized void b1() {
System.out.println("Starting b1");
a.a2();
}
synchronized void b2() {
System.out.println("Starting b2");
}
}
class Thread1 extends Thread {
A a;
Thread1(A a) {
this.a = a;
}
public void run() {
for(int i = 0; i <>
a.a1();
}
}
class Thread2 extends Thread {
B b;
Thread2(B b) {
this.b = b;
}
public void run() {
for(int i = 0; i <>
b.b1();
}
}
class DeadlockDemo {
public static void main(String args[]) {
// Create objects
A a = new A();
B b = new B();
a.b = b;
b.a = a;
// Create threads
Thread1 t1 = new Thread1(a);
Thread2 t2 = new Thread2(b);
t1.start();
t2.start();
// Wait for threads to complete
try {
t1.join();
t2.join();
}
catch(Exception e) {
e.printStackTrace();
}
// Display message
System.out.println("Done!");
}
}
Project of Bank Demo in java
class Account2 {
private int balance;
void deposit(int amount) {
synchronized(this) {
balance += amount;
}
}
int getBalance() {
return balance;
}
}
class Customer2 extends Thread {
Account2 account;
Customer2(Account2 account) {
this.account = account;
}
public void run() {
try {
for(int i = 0; i <>
account.deposit(10);
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
class BankDemo2 {
private final static int NUMCUSTOMERS = 10;
public static void main(String args[]) {
// Create account
Account2 account = new Account2();
// Create and start customer threads
Customer2 customers[] = new Customer2[NUMCUSTOMERS];
for(int i = 0; i <>
customers[i] = new Customer2(account);
customers[i].start();
}
// Wait for customer threads to complete
for(int i = 0; i <>
try {
customers[i].join();
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
// Display account balance
System.out.println(account.getBalance());
}
}
private int balance;
void deposit(int amount) {
synchronized(this) {
balance += amount;
}
}
int getBalance() {
return balance;
}
}
class Customer2 extends Thread {
Account2 account;
Customer2(Account2 account) {
this.account = account;
}
public void run() {
try {
for(int i = 0; i <>
account.deposit(10);
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
class BankDemo2 {
private final static int NUMCUSTOMERS = 10;
public static void main(String args[]) {
// Create account
Account2 account = new Account2();
// Create and start customer threads
Customer2 customers[] = new Customer2[NUMCUSTOMERS];
for(int i = 0; i <>
customers[i] = new Customer2(account);
customers[i].start();
}
// Wait for customer threads to complete
for(int i = 0; i <>
try {
customers[i].join();
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
// Display account balance
System.out.println(account.getBalance());
}
}
Subscribe to:
Posts (Atom)