6 Replies - 4333 Views - Last Post: 20 February 2011 - 11:27 PM Rate Topic: -----

#1 rverma1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-February 11

Reading/Writing Using Threads and Semaphores

Posted 20 February 2011 - 10:48 PM

How to Solve a problem of Reader/Writer problem using a semaphore? or how to do reading and writing using thread in java.

import java.io.*;
import java.util.*;
import java.lang.management.*;


public class rajesh_os1 {

    public static void main(String args[]) throws IOException {
        //... Get two file names from use.
        System.out.println("Enter a filepath to copy from, and one to copy to.");
        Scanner in = new Scanner(System.in);

        //... Create File objects.
        File inFile  = new File(in.next());  // File to read from.
        File outFile = new File(in.next()); // File to write to
        System.out.println("");
        System.out.println("1 FOR LINE BY LINE ");
         BufferedReader b1 = new BufferedReader (new InputStreamReader(System.in));
         int option =0;
        //... Enclose in try..catch because of possible io exceptions.
 
         try
        {
         option = b1.read();
        }
        catch(IOException e) {
            System.out.println("Trying to read");
            System.exit(1);

        }

        if(option=='1')
        {
            System.out.println("LINE BY LINE");
           copyFile(inFile, outFile);

        }
    }
   
    // Line By Line //
    public static void copyFile(File fromFile, File toFile) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(fromFile));
        BufferedWriter writer = new BufferedWriter(new FileWriter(toFile));

        //... Loop as long as there are input lines.
        String line = null;
            long StartTimeW,StartTimeC;
            StartTimeW = System.currentTimeMillis();
            StartTimeC = getCurrentCpuTime();
        while ((line=reader.readLine()) != null) {
            writer.write(line);
            writer.newLine();   // Write system dependent end of line.
        }
        StartTimeW = System.currentTimeMillis() - StartTimeW;
            System.out.println(" The Wall Clock Time is " + StartTimeW + " milliseconds");
            StartTimeC = getCurrentCpuTime() - StartTimeC;
            StartTimeC =(StartTimeC/1000000);
            System.out.println(" The Cpu Clock Time is " + StartTimeC + " milliseconds");
        //... Close reader and writer.
        reader.close();  // Close to unlock.
        writer.close();  // Close to unlock and flush to disk.
    }
    public static long getCurrentCpuTime( ) {
    ThreadMXBean bean = ManagementFactory.getThreadMXBean( );
    return bean.isCurrentThreadCpuTimeSupported( ) ?
        bean.getCurrentThreadCpuTime( ) : 0L;
    }



Edited by macosxnerd101: "Java problem" is not a descriptive title. Also, please use code tags! Like so: :code:.

Is This A Good Question/Topic? 0
  • +

Replies To: Reading/Writing Using Threads and Semaphores

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Reading/Writing Using Threads and Semaphores

Posted 20 February 2011 - 10:54 PM

What's the problem?
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: Reading/Writing Using Threads and Semaphores

Posted 20 February 2011 - 10:56 PM

It seems like most of this code is from Roseindia.

Even so, you're not being clear as to exactly what you are trying to do or what problems/errors you are encountering. Are you trying to read from one File and write to another concurrently? If so, it's a really poor use of Threading. It can be done much more efficiently without Threads.
Was This Post Helpful? 0
  • +
  • -

#4 rverma1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-February 11

Re: Reading/Writing Using Threads and Semaphores

Posted 20 February 2011 - 11:23 PM

View Postmacosxnerd101, on 20 February 2011 - 10:56 PM, said:

It seems like most of this code is from Roseindia.

Even so, you're not being clear as to exactly what you are trying to do or what problems/errors you are encountering. Are you trying to read from one File and write to another concurrently? If so, it's a really poor use of Threading. It can be done much more efficiently without Threads.


i know that but my prof. told me the reading and writing thing using semaphore concept...please help me with this ???

i know that but my prof. told me the reading and writing thing using semaphore concept...please help me with this ???
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: Reading/Writing Using Threads and Semaphores

Posted 20 February 2011 - 11:25 PM

A Semaphore is just a class that determines the number of permits that can be given out. Read each line from the one File into a Queue or Vector. As long as the data structure isn't empty, there should be a permit available for the second Thread to write to the other File. Once the Queue or Vector has been emptied, the writing Thread should return the permit to the Semaphore. When the first Thread finishes reading, and the second Thread finishes writing, your program should terminate.
Was This Post Helpful? 0
  • +
  • -

#6 rverma1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-February 11

Re: Reading/Writing Using Threads and Semaphores

Posted 20 February 2011 - 11:26 PM

can you help me with code ???
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: Reading/Writing Using Threads and Semaphores

Posted 20 February 2011 - 11:27 PM

That's for you to implement. We are happy to help you debug your good faith efforts at implementing a solution, but we will not simply give you the code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1