0% found this document useful (0 votes)
12 views6 pages

Experiment 12 and 13

notes

Uploaded by

tejaswini reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

Experiment 12 and 13

notes

Uploaded by

tejaswini reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

EXPERIMENT 12:

Write a Java program that correctly implements the producer – consumer problem using the
concept of interthread communication.
Source Code:

class ItemQueue
{
int item; boolean valueSet = false; synchronized int getItem()
{
while (!valueSet)
try
{
wait();
}
catch (InterruptedException e)
{
System.out.println("InterruptedException caught");
}
System.out.println("Consummed:" + item);
valueSet = false; try { Thread.sleep(1000);
}
catch (InterruptedException e)
{
System.out.println("InterruptedException caught");
}
notify();
return item;
}
synchronized void putItem(int item)
{
while (valueSet)
try
{
wait();
}
catch (InterruptedException e)
{
System.out.println("InterruptedException caught");
}
this.item = item; valueSet = true; System.out.println("Produced: " + item);
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
System.out.println("InterruptedException caught");
}
notify();
}
}
class Producer implements Runnable
{
ItemQueue itemQueue;
Producer(ItemQueue itemQueue)
{
this.itemQueue = itemQueue;
new Thread(this, "Producer").start();
}
public void run() { int i = 0;
while(true)
{
itemQueue.putItem(i++);
}
}
}
class Consumer implements Runnable
{
ItemQueue itemQueue;
Consumer(ItemQueue itemQueue)
{
this.itemQueue = itemQueue;
new Thread(this, "Consumer").start();
}
public void run()
{
while(true)
{
itemQueue.getItem();
}
}
}
class ProducerConsumer
{
public static void main(String args[])
{
ItemQueue itemQueue = new ItemQueue();
new Producer(itemQueue);
new Consumer(itemQueue);
}
}
Output:

OUTPUT:
Produced: 0
Consummed:0
Produced: 1
Consummed:1
Produced: 2
Consummed:2
Produced: 3
Consummed:3
Produced: 4
Consummed:4
Produced: 5
Consummed:5
Produced: 6
Consummed:6
Produced: 7
Consummed:7
Produced: 8
Consummed:8
Produced: 9
Consummed:9
Produced: 10
Consummed:10
Produced: 11
Consummed:11
Produced: 12
Consummed:12
Produced: 13
Consummed:13
Produced: 14
Consummed:14
Produced: 15
Consummed:15
Produced: 16
Consummed:16
Produced: 17
Consummed:17
Produced: 18
Consummed:18
Produced: 19
Consummed:19
Produced: 20
Consummed:20
Produced: 21
Consummed:21
Produced: 22
Consummed:22
Produced: 23
Consummed:23
Produced: 24
Consummed:24
Produced: 25
Consummed:25
Produced: 26
Consummed:26
Produced: 27
Consummed:27
Ect…..

EXPERIMENT 13:
Write a Java program to list all the files in a directory including the files present in all its
subdirectories

import java.util.Scanner;
import java.io.*;
public class ListingFiles
{
public static void main(String[] args)
{
String path = null; Scanner read = new Scanner(System.in);
System.out.print("Enter the root directory name: ");
path = read.next() + ":\\"; File f_ref = new File(path);
if (!f_ref.exists())
{
printLine();
System.out.println("Root directory does not exists!");
printLine();
}
else
{
String ch = "y"; while (ch.equalsIgnoreCase("y"))
{
printFiles(path);
System.out.print("Do you want to open any sub-directory (Y/N): ");
ch = read.next().toLowerCase();
if (ch.equalsIgnoreCase("y"))
{
System.out.print("Enter the sub-directory name: ");
path = path + "\\\\" + read.next(); File f_ref_2 = new File(path);
if (!f_ref_2.exists())
{
printLine();
System.out.println("The sub-directory does not exists!");
printLine();
int lastIndex = path.lastIndexOf("\\"); path = path.substring(0, lastIndex);
}
}
}
}
System.out.println("***** Program Closed *****");
}
public static void printFiles(String path)
{
System.out.println("Current Location: " + path);
File f_ref = new File(path);
File[] filesList = f_ref.listFiles();
for (File file : filesList)
{
if (file.isFile())
System.out.println("- " + file.getName());
else System.out.println("> " + file.getName());
}
}
public static void printLine()
{
System.out.println("----------------------------------------");
}
}

OUTPUT:

Enter the root directory name: windpws


----------------------------------------
Root directory does not exists!
----------------------------------------
***** Program Closed *****

You might also like