Book Program Java
Book Program Java
String title;
boolean isCompleted;
Book book;
@Override
public void run() {
synchronized (book) {
System.out.println(Thread.currentThread().getName()+" is waiting for the book to be
completed: "+book.getTitle());
try {
book.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+": Book has been completed now!! you
can read it");
}
}
}
class BookWriter implements Runnable{
Book book;
@Override
public void run() {
synchronized (book) {
System.out.println("Author is Starting book TO WRITE : " +book.getTitle() );
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
book.setCompleted(true);
System.out.println("Book has been completed now");
book.notify();
System.out.println("notify one reader");
}
}
}
public class ThreadInterCommunicationMain {
AThread.start();
BThread.start();
e.printStackTrace(); // The printStackTrace() method of Java. lang. Throwable class used to print
this Throwable along with other details like class name and line number where the exception occurred
means its backtrace
}
// BookWriter thread which will notify once book get completed
BookWriter bookWriter=new BookWriter(book);
Thread bookWriterThread=new Thread(bookWriter);
bookWriterThread.start();
Syntax
1. public final void suspend()
Thread class:
Thread class provide constructors and methods to create and perform operations on a
thread.Thread class extends Object class and implements Runnable interface.
Thread()
Thread(String name)
Thread(Runnable r)
Thread(Runnable r,String name)
Runnable interface:
The Runnable interface should be implemented by any class whose instances are intended to be
executed by a thread. Runnable interface have only one method named run().
Starting a thread:
The start() method of Thread class is used to start a newly created thread. It performs the
following tasks:
FileName: Multi3.java
1. Process Synchronization
2. Thread Synchronization
hread Synchronization
There are two types of thread synchronization mutual exclusive and inter-thread communication.
1. Mutual Exclusive
1. Synchronized method.
2. Synchronized block.
3. Static synchronization.
2. Cooperation (Inter-thread communication in java)
Mutual Exclusive
Mutual Exclusive helps keep threads from interfering with one another while sharing data. It can
be achieved by using the following three ways:
Multithreading in Java
Multithreading in Java is a process of executing multiple threads simultaneously.
However, we use multithreading than multiprocessing because threads use a shared memory
area. They don't allocate separate memory area so saves memory, and context-switching between
the threads takes less time than process.
Java Multithreading is mostly used in games, animation, etc.
1) It doesn't block the user because threads are independent and you can perform multiple
operations at the same time.
3) Threads are independent, so it doesn't affect other threads if an exception occurs in a single
thread.