Multiple users want to read multiple files but when one user read
from a file, others must wait until the reading is finished.
Also print the waiting queue
Source Code:
import java.io.*;
class fileread
{
void fread(String s1)throws Exception
{
String str=s1;
FileInputStream ob=new FileInputStream(str);
int x=0;
do
{
x=ob.read();
Thread.sleep(2);
}while(x!=-1);
ob.close();
}
}
class th1 implements Runnable
{
public void run()
{
try{
fileread o1=new fileread();
o1.fread("file1.txt");
}
catch(Exception e){}
}
}
class th2 implements Runnable
{
public void run()
{
try{
fileread o2=new fileread();
o2.fread("file2.txt");
}
catch(Exception e){}
}
}
class th3 implements Runnable
{
public void run()
{
try{
fileread o3=new fileread();
o3.fread("file3.txt");
}
catch(Exception e){}
}
}
class th4 implements Runnable
{
public void run()
{
try{
fileread o4=new fileread();
o4.fread("file4.txt");
}
catch(Exception e){}
}
}
class thmain
{
public static void main(String ars[])throws Exception
{
System.out.print("\nTotal users: User1 User2 User3 User4");
th1 ob1=new th1();
th2 ob2=new th2();
th3 ob3=new th3();
th4 ob4=new th4();
Thread t1=new Thread(ob1);
Thread t2=new Thread(ob2);
Thread t3=new Thread(ob3);
Thread t4=new Thread(ob4);
t1.start();
t2.start();
t3.start();
t4.start();
while(t1.isAlive() || t2.isAlive() || t3.isAlive())
{
if(t1.isAlive())
{
int i=0;
if(t1.isAlive() || (t2.isAlive() || t3.isAlive() ||
t4.isAlive()) && i==0 )
{
t2.suspend();
t3.suspend();
t4.suspend();
System.out.print("\n---------------------------------------
------------------");
System.out.print("\nUser1 is reading file");
System.out.print("\nWaiting queue:");
if(t2.isAlive())
System.out.print(" User2");
if(t3.isAlive())
System.out.print(" User3");
if(t4.isAlive())
System.out.print(" User4");
while(t1.isAlive())
{
}
i=1;
}
System.out.print("\nUser1's file reading complete");
t2.resume();
t3.resume();
t4.resume();
}
if(t2.isAlive())
{
int j=0;
if(t2.isAlive() || (t1.isAlive() || t3.isAlive() ||
t4.isAlive()) && j==0)
{
t1.suspend();
t3.suspend();
t4.suspend();
System.out.print("\n---------------------------------------
------------------");
System.out.print("\nUser2 is reading file");
System.out.print("\nWaiting queue:");
if(t1.isAlive())
System.out.print(" User1");
if(t3.isAlive())
System.out.print(" User3");
if(t4.isAlive())
System.out.print(" User4");
while(t2.isAlive())
{
}
j=1;
}
System.out.print("\nUser2's file reading complete");
t1.resume();
t3.resume();
t4.resume();
}
if(t3.isAlive())
{
int k=0;
if(t3.isAlive() || (t1.isAlive() || t2.isAlive() ||
t4.isAlive()) && k==0)
{
t2.suspend();
t1.suspend();
t4.suspend();
System.out.print("\n---------------------------------------
------------------");
System.out.print("\nUser3 is reading file");
System.out.print("\nWaiting queue:");
if(t1.isAlive())
System.out.print(" User1");
if(t2.isAlive())
System.out.print(" User2");
if(t4.isAlive())
System.out.print(" User4");
while(t3.isAlive())
{
}
k=1;
}
System.out.print("\nUser3's file reading complete");
t2.resume();
t1.resume();
t4.resume();
}
if(t4.isAlive())
{
int m=0;
if(t4.isAlive() || (t1.isAlive() || t3.isAlive() ||
t2.isAlive()) && m==0)
{
t2.suspend();
t3.suspend();
t1.suspend();
System.out.print("\n---------------------------------------
------------------");
System.out.print("\nUser4 is reading file");
System.out.print("\nWaiting queue:");
if(t1.isAlive())
System.out.print(" User1");
if(t2.isAlive())
System.out.print(" User2");
if(t3.isAlive())
System.out.print(" User3");
while(t4.isAlive())
{
}
m=1;
}
System.out.print("\nUser4's file reading complete");
t2.resume();
t3.resume();
t1.resume();
}
}
t1.join();
t2.join();
t3.join();
t4.join();
System.out.println("\nExit From file reading");
}
}
OUTPUT:
D:\Programs>javac newthread.java
Note: newthread.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
D:\Programs>java thmain
Total users: User1 User2 User3 User4
---------------------------------------------------------
User1 is reading file
Waiting queue: User2 User3 User4
User1's file reading complete
---------------------------------------------------------
User2 is reading file
Waiting queue: User3 User4
User2's file reading complete
---------------------------------------------------------
User3 is reading file
Waiting queue: User4
User3's file reading complete
---------------------------------------------------------
User4 is reading file
Waiting queue:
User4's file reading complete
Exit From file reading
0 comments