Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

file accessed by two threads and two objects  RSS feed

 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
scenrio A:

I create two threads and let them access the same File

scenario B:

I have a

class Access {

public void handleFile (File f) {
...
}
}

Then I do
Access a1 = new Access();
Access a2 = new Access();
then I let a1 and a2 do the "handleFile(f)" by passing the same file at the same time.

What's the difference ? can I say that if an object has a synchronized method, then this synchronized method will *only* be taken advantage of by threads, but not by two simultaneous instances that call on this method ?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this what you mean to do next?

If so, everything runs sequentially on one thread. The call to a1 completes, then we call a2. We don't even try to do two things at once.

If Access implemented Runnable it could take the file reference in a constructor and do the handleFile stuff in run().

Now we are attempting to make a1 and a2 access the same file at the same time. This would probably be bad, so we'd synchronize on some object that both threads share ... how about the file?

Only one thread can enter the synchronized block at a time. The first one in will "do the file stuff" and the second will wait. When the first exits the synchronized block the second thread can enter it.

Did that answer the right question?
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
Is this what you mean to do next?

If so, everything runs sequentially on one thread. The call to a1 completes, then we call a2. We don't even try to do two things at once.


WHAT IF I LET a1 and a2 be TimerTask ?? Then I can set the exact same time for them ! Won't they run at the same time then ?


If Access implemented Runnable it could take the file reference in a constructor and do the handleFile stuff in run().

Now we are attempting to make a1 and a2 access the same file at the same time. This would probably be bad, so we'd synchronize on some object that both threads share ... how about the file?

Only one thread can enter the synchronized block at a time. The first one in will "do the file stuff" and the second will wait. When the first exits the synchronized block the second thread can enter it.

Did that answer the right question?
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Access a1 = new Access();
Access a2 = new Access();
a1.handleFile(f);
a2.handleFile(f);

What if I let a1 and a2 be TimerTask ? then I cna set to let them start at the same time, can I ? in this case, are they like multithread ?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

There is no multi-threading in this code snippet. You'll run through a1.handleFile() to completion, then a2.handleFile to completion then whatever is next.

I'm not sure if you're mentioning Timer because you really want to use one or just as an example of multi-threading. If you make Access a TimerTask and schedule two instances a1 and a2 on the same Timer, the Timer will still only run one of them at a time because each Timer instance only uses one thread.

You could make Access a Runnable, queue up a1 and a2 for an Executor and get them run on two threads at once. Then you'd want synchronization just as I showed it above when I explicitly started two threads.
[ July 01, 2006: Message edited by: Stan James ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!