java中的线程
我已经创建了一个子线程,现在我想将一些消息从子线程发送到主线程。如何执行此操作?在您创建的线程中,您将需要一个对您试图向其发送消息(方法调用)的线程的引用 即 MainClass.java:java中的线程,java,Java,我已经创建了一个子线程,现在我想将一些消息从子线程发送到主线程。如何执行此操作?在您创建的线程中,您将需要一个对您试图向其发送消息(方法调用)的线程的引用 即 MainClass.java: public class MainClass implements Runnable { private Queue<String> internalQueue; private boolean keepRunning; public MainClass() {
public class MainClass implements Runnable
{
private Queue<String> internalQueue;
private boolean keepRunning;
public MainClass()
{
keepRunning = true;
internalQueue = new Queue<String>();
}
public void queue(String s)
{
internalQueue.add(s);
this.notify();
}
public void run()
{
// main thread
// create child thread
Runnable r = new YourThread(this);
new Thread().start(r);
// process your queue
while (keepRunning) {
// check if there is something on your queue
// sleep
this.wait();
}
}
public static void main(String[] args)
{
MainClass mc = new MainClass();
mc.run();
}
}
使用优先级队列作为父线程(主线程)和子线程通信的对象。 子级可运行的定义
class CommunicationThead implements Runnable{
Queue<String> commQueue=null;
CommunicationThead(Queue<String> q){
super();
this.commQueue=q;
}
public void run(){
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("child :interuppted on sleep");
}
synchronized(commQueue){
if(commQueue!=null)
{
commQueue.add("Yo");
System.out.println("message added by child");
}
commQueue.notifyAll();
}
}
}
类通信头实现可运行{
队列commQueue=null;
通信头(队列q){
超级();
这个.commQueue=q;
}
公开募捐{
试一试{
睡眠(10000);
}捕捉(中断异常e){
System.out.println(“儿童:睡眠中断”);
}
已同步(通信队列){
if(commQueue!=null)
{
commQueue.add(“Yo”);
System.out.println(“子项添加的消息”);
}
commQueue.notifyAll();
}
}
}
调用子级可运行(在main()中调用)
主线程等待,直到它收到来自子线程的消息
Queue<String> q=new PriorityQueue<String>();
Thread child=new Thread(new CommunicationThead(q));
child.start();
boolean msgReceived=true;
while(msgReceived){
synchronized(q){
if(q.isEmpty())
{
try {
System.out.println("parent: queue empty | parent waiting");
q.wait(1000);
} catch (InterruptedException e) {
System.out.println("parent wait interrupted");
}
}
else{
System.out.println("parent found message :"+q.poll());
msgReceived=false;
}
}
}
Queue q=new PriorityQueue();
线程子线程=新线程(新通信头(q));
child.start();
布尔值msgReceived=true;
while(msgReceived){
同步(q){
if(q.isEmpty())
{
试一试{
System.out.println(“父:队列为空|父等待”);
q、 等待(1000);
}捕捉(中断异常e){
System.out.println(“父等待中断”);
}
}
否则{
System.out.println(“父发现消息:+q.poll());
msgReceived=false;
}
}
}
线程之间没有父子关系
一个线程可以生成另一个线程,一旦生成,这两个线程彼此独立
请让我们知道你所说的发送消息是什么意思。这可以包含广泛的用例,并且每个用例都有其最佳的实现
例如,如果要同步两个线程,可以继续使用简单的等待/通知机制。为此,您必须在两个用户之间共享一个对象
如果您想在生成的线程中计算一个值并将其传回,那么可以使用队列。但是您还必须告诉我们更多关于这两个线程的执行是如何相关的,以便我们可以建议实现它的适当方法。(例如生产者-消费者机制)使用
可调用的
接口,而不是可运行的
公共类MyClass{
public class MyClass {
private MyInterface delegate;
public void setDelegate(MyInterface delegate) {
this.delegate = delegate;
}
// this will be performed in a background thread
public void doStuff() {
Future<String> future = Executors.newSingleThreadExecutor().submit(new Callable<String>() {
@Override
public String call() throws Exception {
return "hello world";
}
});
delegate.handleResponse(future.get());
}
}
public interface MyInterface {
void handleResponse(String value);
}
public class MainClass implements MyInterface {
public static void main(String[] args) {
MyClass myClass = new MyClass();
myClass.setDelegate(this);
myClass.doStuff();
}
@Override
public void handleResponse(String value) {
// this will be on the main thread
System.out.println(value);
}
}
私有接口代表;
公共void setDelegate(MyInterface委托){
this.delegate=委托;
}
//这将在后台线程中执行
公共空间{
Future Future=Executors.newSingleThreadExecutor().submit(new Callable()){
@凌驾
公共字符串调用()引发异常{
返回“你好世界”;
}
});
delegate.handleResponse(future.get());
}
}
公共接口MyInterface{
void handleResponse(字符串值);
}
公共类MainClass实现MyInterface{
公共静态void main(字符串[]args){
MyClass MyClass=新的MyClass();
myClass.setDelegate(这个);
myClass.doStuff();
}
@凌驾
公共void handleResponse(字符串值){
//这将在主线程上
系统输出打印项次(值);
}
}
您已经尝试了什么?有很多种方法,而且没有问题。你所说的信息到底是什么意思?Java线程使用共享内存进行通信,而不是消息传递。当然,您可以使用共享内存模拟消息传递。这就是你要问的吗?+1。我感到惊讶的是,这个答案没有得到更多的支持。选择未来并使用线程池。不要重新发明轮子!
public class MyClass {
private MyInterface delegate;
public void setDelegate(MyInterface delegate) {
this.delegate = delegate;
}
// this will be performed in a background thread
public void doStuff() {
Future<String> future = Executors.newSingleThreadExecutor().submit(new Callable<String>() {
@Override
public String call() throws Exception {
return "hello world";
}
});
delegate.handleResponse(future.get());
}
}
public interface MyInterface {
void handleResponse(String value);
}
public class MainClass implements MyInterface {
public static void main(String[] args) {
MyClass myClass = new MyClass();
myClass.setDelegate(this);
myClass.doStuff();
}
@Override
public void handleResponse(String value) {
// this will be on the main thread
System.out.println(value);
}
}