218k views
5 votes
Your task is to improve a multi-threaded program that frequently writes small entries to a shared file by introducing a layer that merges in memory before writing to file to reduce the number of expensive I/O operations. Imagine a program which writes to file small log entries very frequently. The program is multithreaded and each thread writes log entries to a common log file. To simplify implementation, each thread just writes log individually with blocking I/O interface. Even though threads write to the shared file concurrently without any locking, this is correct because the OS guarantees the atomicity of individual write() operation

1 Answer

7 votes

Answer:

The program is given below

import java.io.IOException;

import java.util.InputMismatchException;

import java.util.Scanner;

public class Add {

public static void main(String[] args)throws IOException

{

int a=0, b=0, sum;

Scanner sc=new Scanner(System.in);

System.out.println("Enter the numbers to be summed");

try{

a=sc.nextInt();

sc.nextLine();

b=sc.nextInt();

}

catch(InputMismatchException e){

System.out.println("Please enter an Integer number");

e.printStackTrace();}

catch(Exception e){System.out.println(e);}

sum=a+b;

System.out.println(sum);

sc.close();

}

}

User Moraltox
by
7.7k points