1

I know that it can be used to run two instances of a class (that implement the Runnable interface) in parallel but can it also be used to run two methods of the same instance in parallel?

We have been working on this paint brush app project using the Java Swing library. We are required to involve multithreading in our project and are not sure where to use it.

B-Mac
  • 11
  • 1
  • 6
  • Of course! There is absolutely no restriction saying that Thread #1 can't call `foo.method1()` while Thread #2 is running `foo.method2()`. If your paint brush app needs some arbitrary multithreading, how about implementing a simple filter like "brighten" that just multiplies all pixels by 1.1 (in parallel). – that other guy Apr 17 '15 at 18:25
  • If I want to achieve that, should I define the threads in those respective methods or inside the main()? – B-Mac Apr 17 '15 at 18:27
  • That depends on what and when the threads should run. If you e.g. want to spawn a thread that downloads an image in the background, you can define it in the action handler of the download button. – that other guy Apr 17 '15 at 18:28
  • You can use them in parallel as long as one does not block the another. – PM 77-1 Apr 17 '15 at 18:29
  • Can you explain a bit more about the project? Does the paint brush app have certain time consuming tasks to perform, like for example a sharpening filter? These kinds of tasks would be ideal for running on a background thread, which has the advantage of keeping the application responsive to user actions. – Freek de Bruijn Apr 17 '15 at 18:33
  • @FreekdeBruijn: No, its a very basic app with functions like....pen, brush and erase. – B-Mac Apr 17 '15 at 18:39
  • Just don't ignore the fact that there are some methods where you won't want to run at the same time on both threads. For cases like this make the header be `public synchronized ReturnType Name(Params)` – Nicholas Eason Apr 17 '15 at 18:40
  • 1
    You could take a look at the following question and answers: [GUI Threading in Java](http://stackoverflow.com/a/9507928/1694043). Even if the current functions are not very complex and time consuming, it is always good to be prepared for it. – Freek de Bruijn Apr 17 '15 at 18:45

0 Answers0