java - What to put inside run() method in concurrent classes? -


suppose have thread-safe class called teacher implements runnable. teacher can either read or write student's book.

public class teacher implements runnable {  boolean donewithbook = false; private lock lock = new reentrantlock(); private condition cond = lock.newcondition();  public void readbook(book book) {      lock.lock();     try {         book.read();         donewithbook = false;         cond.signalall();         system.out.println("teacher read book");     } {         lock.unlock();     } }  public void writetobook(book book) {      lock.lock();     try {         book.write();         donewithbook = true;         system.out.println("teacher wrote book.");     } {         lock.unlock();     } } 

teacher implements runnable, task can ran on own separate thread. don't understand put inside runnable's interface run() method. if want read/write book. how run() come play? examples appreciated.

@override public void run() {    // what???  } 

ok, in general terms it's task implements runnable, in case might have following:

public readtask implements runnable {   private teacher teacher;    public readtask(teacher teacher)   {     this.teacher = teacher;   }    public void run()   {     teacher.readbook();   } }  public writetask implements runnable {   private teacher teacher;    public writetask (teacher teacher)   {     this.teacher = teacher;   }    public void run()   {     teacher.writetobook();   } } 

then calling code this:

teacher teacher = ... new thread(new readtask(teacher)).start(); new thread(new writetask(teacher)).start(); 

also note, condition in code isn't achieving anything.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -