Movie Ticket Booking App 3

 class TotalEarning extends Thread {


  int earning = 0;

  public void run() {
    synchronized (this) {
      for (int i = 0; i < 10; i++) {
        earning += 100;
      }
      this.notify();
    }
  }
}

public class MovieBookApp3 {

  public static void main(String[] args) {
    TotalEarning te = new TotalEarning();
    te.start();
    synchronized (te) {
      try {
        te.wait();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      System.out.println("Total Earning = " + te.earning);
    }
  }
}

Movie Ticket Booking App

 

class BookTheaterSeat {

  static int total_seats = 20;

  //synchronized method
  static synchronized void bookSeat(int seats) {
    if (total_seats >= seats) {
      System.out.println(seats + " Seats booked Successfully");
      total_seats -= seats;
      System.out.println("Total seats available= " + total_seats);
    } else {
      System.out.println("Booking failed due to less seat are available");
      System.out.println("Seats available are : " + total_seats);
    }
  }
}

class MyThread1 extends Thread {

  BookTheaterSeat b;
  int seats;

  MyThread1(BookTheaterSeat b, int seats) {
    this.b = b;
    this.seats = seats;
  }

  @Override
  public void run() {
    b.bookSeat(seats);
  }
}

class MyThread2 extends Thread {

  BookTheaterSeat b;
  int seats;

  MyThread2(BookTheaterSeat b, int seats) {
    this.b = b;
    this.seats = seats;
  }

  @Override
  public void run() {
    b.bookSeat(seats);
  }
}

public class MovieBookApp2 extends Thread {

  int seats;
  static BookTheaterSeat b;

  @Override
  public void run() {
    b.bookSeat(seats);
  }

  public static void main(String[] args) {
    BookTheaterSeat b1 = new BookTheaterSeat();
    MyThread1 t1 = new MyThread1(b1, 7);
    t1.start();

    MyThread2 t2 = new MyThread2(b1, 6);
    t2.start();

    BookTheaterSeat b2 = new BookTheaterSeat();
    MyThread1 t3 = new MyThread1(b2, 5);
    t3.start();

    MyThread2 t4 = new MyThread2(b2, 6);
    t4.start();
  }
}

Movie Ticket Booking App

 

class BookTheaterSeat {

  int total_seats = 10;

  //synchronized method
  synchronized void bookSeat(int seats) {
    if (total_seats >= seats) {
      System.out.println(seats + " Seats booked Successfully");
      total_seats -= seats;
      System.out.println("Total seats available= " + total_seats);
    } else {
      System.out.println("Booking failed less seat are available");
      System.out.println("Seats available are : " + total_seats);
    }
  }
}

public class MovieBookApp extends Thread {

  int seats;
  static BookTheaterSeat b;

  @Override
  public void run() {
    b.bookSeat(seats);
  }

  public static void main(String[] args) {
    b = new BookTheaterSeat();
    MovieBookApp mb1 = new MovieBookApp();
    mb1.seats = 3;
    mb1.start();

    MovieBookApp mb2 = new MovieBookApp();
    mb2.seats = 6;
    mb2.start();
  }
}

Difference between Method and Thread

 public class MethodAndThread extends Thread {


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

  public static void main(String[] args) {
    MethodAndThread mt1 = new MethodAndThread();
    MethodAndThread mt2 = new MethodAndThread();
    //Thread1
    //   mt1.start();
    //Thread2
    //Both threads will run simultaneously
    //  mt2.start();
    //calling run() method
    mt1.run();
    //calling run() method
    //method will run one after another
    mt2.run();
  }
}

Thread Joining

 

public class JoinMainThread extends Thread {

  static Thread mainthread;

  public void run() {
    try {
      mainthread.join();
      for (int i = 0; i < 5; i++) {
        System.out.println("child thread : " + i);
        Thread.sleep(1000);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    mainthread = Thread.currentThread();
    JoinMainThread jm = new JoinMainThread();
    jm.start();

    try {
      for (int i = 0; i < 5; i++) {
        System.out.println("main thread : " + i);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Thread Interruption

 

public class IsInterrupted extends Thread {
  @Override
  public void run() {
    //change the status of interrupted from true to false
    //only if thread is interrupted
    //interrupted() is a static method
    //can be invoked using thread.interrupted()
    System.out.println(Thread.interrupted()); //true->false
    //does not change the interrupted status
    //only gives the status
    //not a static method
    //have to be invoked using currentThread()
    System.out.println(Thread.currentThread().isInterrupted());
    try {
      for (int i = 1; i <= 5; i++) {
        System.out.println(i);
        Thread.sleep(1000);
      }
    } catch (Exception e) {
      System.out.println("Thread interrupted : " + e);
    }
  }

  public static void main(String[] args) {
    IsInterrupted ti = new IsInterrupted();
    ti.start();
    // ti.interrupt();
  }
}

Daemon Thread

 

public class DaemonThread extends Thread {

  @Override
  public void run() {
    System.out.println("inside user created thread");

    if (Thread.currentThread().isDaemon()) {
      System.out.println("It is a Daemon Thread");
    } else {
      System.out.println("It is not a daemon thread");
    }
  }

  public static void main(String[] args) {
    System.out.println("main thread");
    //We can not set a thread as daemon thread after running
    //that why this will give exeption for main
    //i.e we can not set main thread as deamon thread
    //Thread.currentThread().setDaemon(true);
    DaemonThread dt = new DaemonThread();
    dt.setDaemon(true);
    dt.start();
    // System.out.println(dt.isAlive());
  }
}

Using Yield Method in a Thread

 

public class Yield extends Thread {

  public void run() {
    //to stop thread 0
    Thread.yield();
    for (int i = 0; i < 5; i++) {
      System.out.println(i + ":" + Thread.currentThread().getName());
    }
  }

  public static void main(String[] args) {
    Yield y1 = new Yield();
    y1.start();
    //to stop main method and other thread get a chance to execute
    Thread.yield();
    for (int i = 0; i < 5; i++) {
      System.out.println(Thread.currentThread().getName() + ":" + i);
    }
  }
}

Performing sngle task using Multiple Thread

 

//Performing single task using multiple thread
public class SingleTaskMultipleThread extends Thread {

  @Override
  public void run() {
    System.out.println("Perfroming task");
  }

  //driver method
  public static void main(String[] args) {
    //thread1
    SingleTaskMultipleThread stmt1 = new SingleTaskMultipleThread();
    //invoking thread1
    stmt1.start();
    //thread2
    SingleTaskMultipleThread stmt2 = new SingleTaskMultipleThread();
    //invoking thread2
    stmt2.start();
    //thread3
    SingleTaskMultipleThread stmt3 = new SingleTaskMultipleThread();
    //invoking thread3
    stmt3.start();
  }
}

Thread Naming

 

class ThreadNaming extends Thread {

  ThreadNaming(String threadName) {
    super(threadName);
  }

  public void run() {
    System.out.println("Thread is running");
  }
}

public class NamingThread {

  public static void main(String[] args) {
    ThreadNaming tn1 = new ThreadNaming("java");
    ThreadNaming tn2 = new ThreadNaming("Invent");

    System.out.println("Thread th1 name : " + tn1.getName());
    System.out.println("Thread th2 name : " + tn2.getName());

    System.out.println("Thread th1 id : " + tn1.getId());
    System.out.println("Thread th2 id : " + tn2.getId());

    System.out.println(
      "Thread main Priority : " + Thread.currentThread().getPriority()
    );
    System.out.println("Thread th1 : " + tn1.getPriority());
    System.out.println("Thread th2 : " + tn2.getPriority());
  }
}

Google Script for Data Entry Form in Google Spreadsheet

// function to validate the entry made by user in user form function validateEntry (){ // declare a variable and referernece of active goog...