简体   繁体   中英

priority based update of file data and multithreading in Java

i wan't to modify a file using multiple threads at any given time given the constraints that

  • each thread has a priority either normal or max.

  • file contains a table (let say marks of students(name,roll no.) in a particular subject).

  • any line which can be modified by a normal priority thread can be later modified by any thread.
  • once a line is modified by a max priority thread , the normal priority threads can't modify that particular line .

scheduling a threads to access file is doable but restricting some line from being modified by some threads is quite difficult for me . how can i accomplish above task in java ? please answer my question considering i'm very new to java , multithreading/concurrent programming and file parsing in java . thanks in advance.

As per my understanding the design can be:

class ToothBrushHolder{
   List<ToothBrush> toothBrushList;
   ToothBrush pickToothBrush{
        ToothBrush:pick the tooth brush from list
         }
   void useToothBrush(ToothBrush toothBrush){
        synchronize(toothBrush){
        if(toothBrush.getLastUsedBy!= "particularThreadName"){
          //perform the action
           toothBrush.setLastUsedBy("ThreadName);
      }else
        { can't use brush
           }
 }
 }

class ToothBrush{
    String ToothBrushID;
    String LastUsedBy;//setThreadname
   }

Correctly controlling concurrent modification of specific lines in a file may be particularly difficult, given that you're new to concurrency in Java.

If the file is not too large, it might be easier to read it into a standard data structure (see: https://docs.oracle.com/javase/8/docs/api/index.html?java/util/concurrent/package-summary.html ) that provides guaranteed atomic operations, or to create your own data structure and implement a synchronized method for updating that data structure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM