From 218550add2298d3ad40f4b21170ec79cf1754d2f Mon Sep 17 00:00:00 2001 From: siddheshmhatrecollege Date: Thu, 29 Jan 2026 14:27:35 +0530 Subject: [PATCH] 6 and 7th --- MovieBook.java | 24 ++++++++++++++++++++++++ Sync6.java | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 MovieBook.java create mode 100644 Sync6.java diff --git a/MovieBook.java b/MovieBook.java new file mode 100644 index 0000000..7cb4c61 --- /dev/null +++ b/MovieBook.java @@ -0,0 +1,24 @@ +// 7: write a java program for interthread communication using wait(), notify(), notifyAll() +class TotalEarnings extends Thread{ + int total = 0; + public void run(){ + synchronized(this){ + for(int i=0;i<=10;i++){ + total = total + 100; + } + this.notify(); + } + } +} + +public class MovieBook{ + public static void main(String[] args) throws InterruptedException{ + TotalEarnings te = new TotalEarnings(); + te.start(); + synchronized(te){ + te.wait(); + System.out.println("total earnings: " + te.total); + + } + } +} diff --git a/Sync6.java b/Sync6.java new file mode 100644 index 0000000..f53215f --- /dev/null +++ b/Sync6.java @@ -0,0 +1,44 @@ +// 6 write a program for demonstrating thread synchronization. +class Table { + synchronized void printTable(int n) { + for(int i = 1; i<=5; i++) { + System.out.println(n * i); + try{ + Thread.sleep(400); + }catch(Exception e){ + System.out.println(e); + } + } + } +} + +class Thread1 extends Thread{ + Table t; + Thread1(Table t){ + this.t = t; + } + public void run(){ + t.printTable(5); + } +} + +class Thread2 extends Thread{ + Table t; + Thread2(Table t){ + this.t = t; + } + public void run(){ + t.printTable(100); + + } +} + +class Sync6{ + public static void main(String arg[]){ + Table obj = new Table(); + Thread1 t1 = new Thread1(obj); + Thread2 t2 = new Thread2(obj); + t1.start(); + t2.start(); + } +}