inter thread communication in java Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Events between threads in javaHow do I call a method from one thread but have another thread execute the method in Java?Is “Java Concurrency In Practice” still valid?Communication between java threads: stopping all threads when one finishes its taskHow to notify main thread on exceptions occuring in scheduledExecutor task threads?Inter-threads communicationJava thread monitor other threadsJava Inter Thread CommunicationWhen communicating with two threads do I have to use pipes?How to stop the printing in thread A from thread B?What is a race condition?Is Java “pass-by-reference” or “pass-by-value”?How do I read / convert an InputStream into a String in Java?When to use LinkedList over ArrayList in Java?How do I generate random integers within a specific range in Java?“implements Runnable” vs “extends Thread” in JavaHow do I update the GUI from another thread?Converting array to list in JavaHow do I convert a String to an int in Java?Creating a memory leak with Java
Who said what about *meanings*?
First paper to introduce the "principal-agent problem"
3D Masyu - A Die
Keep at all times, the minus sign above aligned with minus sign below
Fit odd number of triplets in a measure?
Are there any irrational/transcendental numbers for which the distribution of decimal digits is not uniform?
Did any compiler fully use 80-bit floating point?
Found this skink in my tomato plant bucket. Is he trapped? Or could he leave if he wanted?
What did Turing mean when saying that "machines cannot give rise to surprises" is due to a fallacy?
"Destructive power" carried by a B-52?
Does the main washing effect of soap come from foam?
.bashrc alias for a command with fixed second parameter
One-one communication
malloc in main() or malloc in another function: allocating memory for a struct and its members
As a dual citizen, my US passport will expire one day after traveling to the US. Will this work?
Why did Bronn offer to be Tyrion Lannister's champion in trial by combat?
Is there any significance to the prison numbers of the Beagle Boys starting with 176-?
How much damage would a cupful of neutron star matter do to the Earth?
Why does BitLocker not use RSA?
Does the transliteration of 'Dravidian' exist in Hindu scripture? Does 'Dravida' refer to a Geographical area or an ethnic group?
Is it OK to use the testing sample to compare algorithms?
How do Java 8 default methods hеlp with lambdas?
Why complex landing gears are used instead of simple, reliable and light weight muscle wire or shape memory alloys?
What is the proper term for etching or digging of wall to hide conduit of cables
inter thread communication in java
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Events between threads in javaHow do I call a method from one thread but have another thread execute the method in Java?Is “Java Concurrency In Practice” still valid?Communication between java threads: stopping all threads when one finishes its taskHow to notify main thread on exceptions occuring in scheduledExecutor task threads?Inter-threads communicationJava thread monitor other threadsJava Inter Thread CommunicationWhen communicating with two threads do I have to use pipes?How to stop the printing in thread A from thread B?What is a race condition?Is Java “pass-by-reference” or “pass-by-value”?How do I read / convert an InputStream into a String in Java?When to use LinkedList over ArrayList in Java?How do I generate random integers within a specific range in Java?“implements Runnable” vs “extends Thread” in JavaHow do I update the GUI from another thread?Converting array to list in JavaHow do I convert a String to an int in Java?Creating a memory leak with Java
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
How do threads that rely on one another communicate in Java?
For example, I am building a web crawler with threads that need data that comes from other threads.
java multithreading
add a comment |
How do threads that rely on one another communicate in Java?
For example, I am building a web crawler with threads that need data that comes from other threads.
java multithreading
add a comment |
How do threads that rely on one another communicate in Java?
For example, I am building a web crawler with threads that need data that comes from other threads.
java multithreading
How do threads that rely on one another communicate in Java?
For example, I am building a web crawler with threads that need data that comes from other threads.
java multithreading
java multithreading
asked Jan 31 '10 at 4:05
tiputipu
3,90595292
3,90595292
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
That depends on the nature of the communication.
- Is it duplex (ie A talks to B and B talks to A)?
- Is it communication of data or communication of completion?
- and so on.
The simplest and most advisable form of inter-thread communication is simply to wait for the completion of other threads. That's most easily done by using Future:
ExecutorService exec = Executors.newFixedThreadPool(50);
final Future f = exec.submit(task1);
exec.submit(new Runnable()
@Override
public void run()
f.get();
// do stuff
);
The second task won't execute until the first completes.
Java 5+ has many concurrent utilities for dealing with this kind of thing. This could mean using LinkedBlockingQueues, CountDownLatch or many, many others.
For an in-depth examination of concurrency Java Concurrency in Practice is a must-read.
The threads don't go both ways, one thread waits for the result of another but not vice versa. Admittedly I'm a bit intimidated by the use of ExecutorService and Future, as I've never heard of them before. Is there another method of interthread communication not using Future and ExecutorService?
– tipu
Jan 31 '10 at 4:50
1
There's no need to be intimidated byExecutorServiceandFuture. They are reasonably simple and should take less than 2 hours to learn. UsingFutures is easier than the standard queue classes, and a lot easier than rolling your own queue classes usingwait()andnotify().
– finnw
Jan 31 '10 at 5:04
@cletus, -1 for callingFuture.get()from within a task in a fixed thread pool. The danger there should be obvious.
– finnw
Jan 31 '10 at 5:08
@finnw: of course theres a danger but in the absence of the requirements for the communication or a better description you can't possibly suggest a method without danger.
– cletus
Jan 31 '10 at 5:16
This particular danger can be rectified by using a cached thread pool.
– finnw
Jan 31 '10 at 5:49
|
show 4 more comments
Below is example for Inter thread communication:
public class Main
public static void main(String[] args)
Chat m = new Chat();
new T1(m);
new T2(m);
class Chat
boolean flag = false;
public synchronized void FromSam(String msg)
if (flag)
try
wait();
catch (InterruptedException e)
e.printStackTrace();
System.out.println(msg);
flag = true;
notify();
public synchronized void FromJam(String msg)
if (!flag)
try
wait();
catch (InterruptedException e)
e.printStackTrace();
System.out.println(msg);
flag = false;
notify();
class T1 implements Runnable
Chat m;
String[] s1 = "Hello Jam", "How are you ?", "I am also doing fine!" ;
public T1(Chat m1)
this.m = m1;
new Thread(this, "Sam").start();
public void run()
for (int i = 0; i < s1.length; i++)
m.FromSam(s1[i]);
class T2 implements Runnable
Chat m;
String[] s2 = "HI Sam", "I am good,And U ?", "ha haa" ;
public T2(Chat m2)
this.m = m2;
new Thread(this, "Jam").start();
public void run()
for (int i = 0; i < s2.length; i++)
m.FromJam(s2[i]);
2
This example is used here: http://www.tutorialspoint.com
– Malus Jan
Jul 3 '16 at 20:59
add a comment |
Take a look at java.util.Observer/java.util.Observable.
They are exactly what you are looking for.
6
Using observers will not work, as they don't cross threads (all Observer callbacks will be called from the Observable's thread). "Note that this notification mechanism is has nothing to do with threads." from docs.oracle.com/javase/6/docs/api/java/util/Observable.html
– Wander Nauta
Jul 31 '13 at 8:00
add a comment |
You could use 2 ExecutorServices, submit a Callable implementation to service A that knows about follow on service B. When the Callable has completed it's work, it submits a new task to service B that does something additional with the results.
You could use a CountDownLatch or some other barrier if you need to perform additional work once all items have been processed in both services.
The ExecutorService api is pretty straight forward, for the most part you'll probably be using something like .newFixedThreadPool(int threads) and submitting Runnables/Callables to it.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2170520%2finter-thread-communication-in-java%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
That depends on the nature of the communication.
- Is it duplex (ie A talks to B and B talks to A)?
- Is it communication of data or communication of completion?
- and so on.
The simplest and most advisable form of inter-thread communication is simply to wait for the completion of other threads. That's most easily done by using Future:
ExecutorService exec = Executors.newFixedThreadPool(50);
final Future f = exec.submit(task1);
exec.submit(new Runnable()
@Override
public void run()
f.get();
// do stuff
);
The second task won't execute until the first completes.
Java 5+ has many concurrent utilities for dealing with this kind of thing. This could mean using LinkedBlockingQueues, CountDownLatch or many, many others.
For an in-depth examination of concurrency Java Concurrency in Practice is a must-read.
The threads don't go both ways, one thread waits for the result of another but not vice versa. Admittedly I'm a bit intimidated by the use of ExecutorService and Future, as I've never heard of them before. Is there another method of interthread communication not using Future and ExecutorService?
– tipu
Jan 31 '10 at 4:50
1
There's no need to be intimidated byExecutorServiceandFuture. They are reasonably simple and should take less than 2 hours to learn. UsingFutures is easier than the standard queue classes, and a lot easier than rolling your own queue classes usingwait()andnotify().
– finnw
Jan 31 '10 at 5:04
@cletus, -1 for callingFuture.get()from within a task in a fixed thread pool. The danger there should be obvious.
– finnw
Jan 31 '10 at 5:08
@finnw: of course theres a danger but in the absence of the requirements for the communication or a better description you can't possibly suggest a method without danger.
– cletus
Jan 31 '10 at 5:16
This particular danger can be rectified by using a cached thread pool.
– finnw
Jan 31 '10 at 5:49
|
show 4 more comments
That depends on the nature of the communication.
- Is it duplex (ie A talks to B and B talks to A)?
- Is it communication of data or communication of completion?
- and so on.
The simplest and most advisable form of inter-thread communication is simply to wait for the completion of other threads. That's most easily done by using Future:
ExecutorService exec = Executors.newFixedThreadPool(50);
final Future f = exec.submit(task1);
exec.submit(new Runnable()
@Override
public void run()
f.get();
// do stuff
);
The second task won't execute until the first completes.
Java 5+ has many concurrent utilities for dealing with this kind of thing. This could mean using LinkedBlockingQueues, CountDownLatch or many, many others.
For an in-depth examination of concurrency Java Concurrency in Practice is a must-read.
The threads don't go both ways, one thread waits for the result of another but not vice versa. Admittedly I'm a bit intimidated by the use of ExecutorService and Future, as I've never heard of them before. Is there another method of interthread communication not using Future and ExecutorService?
– tipu
Jan 31 '10 at 4:50
1
There's no need to be intimidated byExecutorServiceandFuture. They are reasonably simple and should take less than 2 hours to learn. UsingFutures is easier than the standard queue classes, and a lot easier than rolling your own queue classes usingwait()andnotify().
– finnw
Jan 31 '10 at 5:04
@cletus, -1 for callingFuture.get()from within a task in a fixed thread pool. The danger there should be obvious.
– finnw
Jan 31 '10 at 5:08
@finnw: of course theres a danger but in the absence of the requirements for the communication or a better description you can't possibly suggest a method without danger.
– cletus
Jan 31 '10 at 5:16
This particular danger can be rectified by using a cached thread pool.
– finnw
Jan 31 '10 at 5:49
|
show 4 more comments
That depends on the nature of the communication.
- Is it duplex (ie A talks to B and B talks to A)?
- Is it communication of data or communication of completion?
- and so on.
The simplest and most advisable form of inter-thread communication is simply to wait for the completion of other threads. That's most easily done by using Future:
ExecutorService exec = Executors.newFixedThreadPool(50);
final Future f = exec.submit(task1);
exec.submit(new Runnable()
@Override
public void run()
f.get();
// do stuff
);
The second task won't execute until the first completes.
Java 5+ has many concurrent utilities for dealing with this kind of thing. This could mean using LinkedBlockingQueues, CountDownLatch or many, many others.
For an in-depth examination of concurrency Java Concurrency in Practice is a must-read.
That depends on the nature of the communication.
- Is it duplex (ie A talks to B and B talks to A)?
- Is it communication of data or communication of completion?
- and so on.
The simplest and most advisable form of inter-thread communication is simply to wait for the completion of other threads. That's most easily done by using Future:
ExecutorService exec = Executors.newFixedThreadPool(50);
final Future f = exec.submit(task1);
exec.submit(new Runnable()
@Override
public void run()
f.get();
// do stuff
);
The second task won't execute until the first completes.
Java 5+ has many concurrent utilities for dealing with this kind of thing. This could mean using LinkedBlockingQueues, CountDownLatch or many, many others.
For an in-depth examination of concurrency Java Concurrency in Practice is a must-read.
edited Feb 6 '12 at 0:20
Cameron Skinner
37k25577
37k25577
answered Jan 31 '10 at 4:11
cletuscletus
513k141844915
513k141844915
The threads don't go both ways, one thread waits for the result of another but not vice versa. Admittedly I'm a bit intimidated by the use of ExecutorService and Future, as I've never heard of them before. Is there another method of interthread communication not using Future and ExecutorService?
– tipu
Jan 31 '10 at 4:50
1
There's no need to be intimidated byExecutorServiceandFuture. They are reasonably simple and should take less than 2 hours to learn. UsingFutures is easier than the standard queue classes, and a lot easier than rolling your own queue classes usingwait()andnotify().
– finnw
Jan 31 '10 at 5:04
@cletus, -1 for callingFuture.get()from within a task in a fixed thread pool. The danger there should be obvious.
– finnw
Jan 31 '10 at 5:08
@finnw: of course theres a danger but in the absence of the requirements for the communication or a better description you can't possibly suggest a method without danger.
– cletus
Jan 31 '10 at 5:16
This particular danger can be rectified by using a cached thread pool.
– finnw
Jan 31 '10 at 5:49
|
show 4 more comments
The threads don't go both ways, one thread waits for the result of another but not vice versa. Admittedly I'm a bit intimidated by the use of ExecutorService and Future, as I've never heard of them before. Is there another method of interthread communication not using Future and ExecutorService?
– tipu
Jan 31 '10 at 4:50
1
There's no need to be intimidated byExecutorServiceandFuture. They are reasonably simple and should take less than 2 hours to learn. UsingFutures is easier than the standard queue classes, and a lot easier than rolling your own queue classes usingwait()andnotify().
– finnw
Jan 31 '10 at 5:04
@cletus, -1 for callingFuture.get()from within a task in a fixed thread pool. The danger there should be obvious.
– finnw
Jan 31 '10 at 5:08
@finnw: of course theres a danger but in the absence of the requirements for the communication or a better description you can't possibly suggest a method without danger.
– cletus
Jan 31 '10 at 5:16
This particular danger can be rectified by using a cached thread pool.
– finnw
Jan 31 '10 at 5:49
The threads don't go both ways, one thread waits for the result of another but not vice versa. Admittedly I'm a bit intimidated by the use of ExecutorService and Future, as I've never heard of them before. Is there another method of interthread communication not using Future and ExecutorService?
– tipu
Jan 31 '10 at 4:50
The threads don't go both ways, one thread waits for the result of another but not vice versa. Admittedly I'm a bit intimidated by the use of ExecutorService and Future, as I've never heard of them before. Is there another method of interthread communication not using Future and ExecutorService?
– tipu
Jan 31 '10 at 4:50
1
1
There's no need to be intimidated by
ExecutorService and Future. They are reasonably simple and should take less than 2 hours to learn. Using Futures is easier than the standard queue classes, and a lot easier than rolling your own queue classes using wait() and notify().– finnw
Jan 31 '10 at 5:04
There's no need to be intimidated by
ExecutorService and Future. They are reasonably simple and should take less than 2 hours to learn. Using Futures is easier than the standard queue classes, and a lot easier than rolling your own queue classes using wait() and notify().– finnw
Jan 31 '10 at 5:04
@cletus, -1 for calling
Future.get() from within a task in a fixed thread pool. The danger there should be obvious.– finnw
Jan 31 '10 at 5:08
@cletus, -1 for calling
Future.get() from within a task in a fixed thread pool. The danger there should be obvious.– finnw
Jan 31 '10 at 5:08
@finnw: of course theres a danger but in the absence of the requirements for the communication or a better description you can't possibly suggest a method without danger.
– cletus
Jan 31 '10 at 5:16
@finnw: of course theres a danger but in the absence of the requirements for the communication or a better description you can't possibly suggest a method without danger.
– cletus
Jan 31 '10 at 5:16
This particular danger can be rectified by using a cached thread pool.
– finnw
Jan 31 '10 at 5:49
This particular danger can be rectified by using a cached thread pool.
– finnw
Jan 31 '10 at 5:49
|
show 4 more comments
Below is example for Inter thread communication:
public class Main
public static void main(String[] args)
Chat m = new Chat();
new T1(m);
new T2(m);
class Chat
boolean flag = false;
public synchronized void FromSam(String msg)
if (flag)
try
wait();
catch (InterruptedException e)
e.printStackTrace();
System.out.println(msg);
flag = true;
notify();
public synchronized void FromJam(String msg)
if (!flag)
try
wait();
catch (InterruptedException e)
e.printStackTrace();
System.out.println(msg);
flag = false;
notify();
class T1 implements Runnable
Chat m;
String[] s1 = "Hello Jam", "How are you ?", "I am also doing fine!" ;
public T1(Chat m1)
this.m = m1;
new Thread(this, "Sam").start();
public void run()
for (int i = 0; i < s1.length; i++)
m.FromSam(s1[i]);
class T2 implements Runnable
Chat m;
String[] s2 = "HI Sam", "I am good,And U ?", "ha haa" ;
public T2(Chat m2)
this.m = m2;
new Thread(this, "Jam").start();
public void run()
for (int i = 0; i < s2.length; i++)
m.FromJam(s2[i]);
2
This example is used here: http://www.tutorialspoint.com
– Malus Jan
Jul 3 '16 at 20:59
add a comment |
Below is example for Inter thread communication:
public class Main
public static void main(String[] args)
Chat m = new Chat();
new T1(m);
new T2(m);
class Chat
boolean flag = false;
public synchronized void FromSam(String msg)
if (flag)
try
wait();
catch (InterruptedException e)
e.printStackTrace();
System.out.println(msg);
flag = true;
notify();
public synchronized void FromJam(String msg)
if (!flag)
try
wait();
catch (InterruptedException e)
e.printStackTrace();
System.out.println(msg);
flag = false;
notify();
class T1 implements Runnable
Chat m;
String[] s1 = "Hello Jam", "How are you ?", "I am also doing fine!" ;
public T1(Chat m1)
this.m = m1;
new Thread(this, "Sam").start();
public void run()
for (int i = 0; i < s1.length; i++)
m.FromSam(s1[i]);
class T2 implements Runnable
Chat m;
String[] s2 = "HI Sam", "I am good,And U ?", "ha haa" ;
public T2(Chat m2)
this.m = m2;
new Thread(this, "Jam").start();
public void run()
for (int i = 0; i < s2.length; i++)
m.FromJam(s2[i]);
2
This example is used here: http://www.tutorialspoint.com
– Malus Jan
Jul 3 '16 at 20:59
add a comment |
Below is example for Inter thread communication:
public class Main
public static void main(String[] args)
Chat m = new Chat();
new T1(m);
new T2(m);
class Chat
boolean flag = false;
public synchronized void FromSam(String msg)
if (flag)
try
wait();
catch (InterruptedException e)
e.printStackTrace();
System.out.println(msg);
flag = true;
notify();
public synchronized void FromJam(String msg)
if (!flag)
try
wait();
catch (InterruptedException e)
e.printStackTrace();
System.out.println(msg);
flag = false;
notify();
class T1 implements Runnable
Chat m;
String[] s1 = "Hello Jam", "How are you ?", "I am also doing fine!" ;
public T1(Chat m1)
this.m = m1;
new Thread(this, "Sam").start();
public void run()
for (int i = 0; i < s1.length; i++)
m.FromSam(s1[i]);
class T2 implements Runnable
Chat m;
String[] s2 = "HI Sam", "I am good,And U ?", "ha haa" ;
public T2(Chat m2)
this.m = m2;
new Thread(this, "Jam").start();
public void run()
for (int i = 0; i < s2.length; i++)
m.FromJam(s2[i]);
Below is example for Inter thread communication:
public class Main
public static void main(String[] args)
Chat m = new Chat();
new T1(m);
new T2(m);
class Chat
boolean flag = false;
public synchronized void FromSam(String msg)
if (flag)
try
wait();
catch (InterruptedException e)
e.printStackTrace();
System.out.println(msg);
flag = true;
notify();
public synchronized void FromJam(String msg)
if (!flag)
try
wait();
catch (InterruptedException e)
e.printStackTrace();
System.out.println(msg);
flag = false;
notify();
class T1 implements Runnable
Chat m;
String[] s1 = "Hello Jam", "How are you ?", "I am also doing fine!" ;
public T1(Chat m1)
this.m = m1;
new Thread(this, "Sam").start();
public void run()
for (int i = 0; i < s1.length; i++)
m.FromSam(s1[i]);
class T2 implements Runnable
Chat m;
String[] s2 = "HI Sam", "I am good,And U ?", "ha haa" ;
public T2(Chat m2)
this.m = m2;
new Thread(this, "Jam").start();
public void run()
for (int i = 0; i < s2.length; i++)
m.FromJam(s2[i]);
edited Feb 6 '12 at 0:30
CSchulz
6,03584592
6,03584592
answered Jan 19 '11 at 12:34
SujaySujay
28132
28132
2
This example is used here: http://www.tutorialspoint.com
– Malus Jan
Jul 3 '16 at 20:59
add a comment |
2
This example is used here: http://www.tutorialspoint.com
– Malus Jan
Jul 3 '16 at 20:59
2
2
This example is used here: http://www.tutorialspoint.com
– Malus Jan
Jul 3 '16 at 20:59
This example is used here: http://www.tutorialspoint.com
– Malus Jan
Jul 3 '16 at 20:59
add a comment |
Take a look at java.util.Observer/java.util.Observable.
They are exactly what you are looking for.
6
Using observers will not work, as they don't cross threads (all Observer callbacks will be called from the Observable's thread). "Note that this notification mechanism is has nothing to do with threads." from docs.oracle.com/javase/6/docs/api/java/util/Observable.html
– Wander Nauta
Jul 31 '13 at 8:00
add a comment |
Take a look at java.util.Observer/java.util.Observable.
They are exactly what you are looking for.
6
Using observers will not work, as they don't cross threads (all Observer callbacks will be called from the Observable's thread). "Note that this notification mechanism is has nothing to do with threads." from docs.oracle.com/javase/6/docs/api/java/util/Observable.html
– Wander Nauta
Jul 31 '13 at 8:00
add a comment |
Take a look at java.util.Observer/java.util.Observable.
They are exactly what you are looking for.
Take a look at java.util.Observer/java.util.Observable.
They are exactly what you are looking for.
edited Mar 22 '10 at 14:41
Kieveli
8,98564171
8,98564171
answered Jan 31 '10 at 5:28
David SowsyDavid Sowsy
1,6501413
1,6501413
6
Using observers will not work, as they don't cross threads (all Observer callbacks will be called from the Observable's thread). "Note that this notification mechanism is has nothing to do with threads." from docs.oracle.com/javase/6/docs/api/java/util/Observable.html
– Wander Nauta
Jul 31 '13 at 8:00
add a comment |
6
Using observers will not work, as they don't cross threads (all Observer callbacks will be called from the Observable's thread). "Note that this notification mechanism is has nothing to do with threads." from docs.oracle.com/javase/6/docs/api/java/util/Observable.html
– Wander Nauta
Jul 31 '13 at 8:00
6
6
Using observers will not work, as they don't cross threads (all Observer callbacks will be called from the Observable's thread). "Note that this notification mechanism is has nothing to do with threads." from docs.oracle.com/javase/6/docs/api/java/util/Observable.html
– Wander Nauta
Jul 31 '13 at 8:00
Using observers will not work, as they don't cross threads (all Observer callbacks will be called from the Observable's thread). "Note that this notification mechanism is has nothing to do with threads." from docs.oracle.com/javase/6/docs/api/java/util/Observable.html
– Wander Nauta
Jul 31 '13 at 8:00
add a comment |
You could use 2 ExecutorServices, submit a Callable implementation to service A that knows about follow on service B. When the Callable has completed it's work, it submits a new task to service B that does something additional with the results.
You could use a CountDownLatch or some other barrier if you need to perform additional work once all items have been processed in both services.
The ExecutorService api is pretty straight forward, for the most part you'll probably be using something like .newFixedThreadPool(int threads) and submitting Runnables/Callables to it.
add a comment |
You could use 2 ExecutorServices, submit a Callable implementation to service A that knows about follow on service B. When the Callable has completed it's work, it submits a new task to service B that does something additional with the results.
You could use a CountDownLatch or some other barrier if you need to perform additional work once all items have been processed in both services.
The ExecutorService api is pretty straight forward, for the most part you'll probably be using something like .newFixedThreadPool(int threads) and submitting Runnables/Callables to it.
add a comment |
You could use 2 ExecutorServices, submit a Callable implementation to service A that knows about follow on service B. When the Callable has completed it's work, it submits a new task to service B that does something additional with the results.
You could use a CountDownLatch or some other barrier if you need to perform additional work once all items have been processed in both services.
The ExecutorService api is pretty straight forward, for the most part you'll probably be using something like .newFixedThreadPool(int threads) and submitting Runnables/Callables to it.
You could use 2 ExecutorServices, submit a Callable implementation to service A that knows about follow on service B. When the Callable has completed it's work, it submits a new task to service B that does something additional with the results.
You could use a CountDownLatch or some other barrier if you need to perform additional work once all items have been processed in both services.
The ExecutorService api is pretty straight forward, for the most part you'll probably be using something like .newFixedThreadPool(int threads) and submitting Runnables/Callables to it.
answered Feb 3 '10 at 3:27
ChrisChris
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2170520%2finter-thread-communication-in-java%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown