run()方法和其他类方法之间的通信(java线程) [英] communication between run() method and other class method (java threads)

查看:79
本文介绍了run()方法和其他类方法之间的通信(java线程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有任务要做,我很少受困.我必须提供4个服务(A,B,C,D).每个服务都应该有自己的线程.它们应按顺序启动并运行.如果服务A启动则可以启动服务B,如果服务B启动如果服务C启动则可以启动服务D.我设法创建服务及其线程,但是我不知道如何在start()和priority()方法之间创建通信在PriorityService类中.我想检查service(thread)A是否仍在运行,是否要从列表移至第二个服务,依此类推.那可能吗?您还有其他想法如何编写服务依赖项吗? 任何建议都是有用的. Tnx.

i have task to do and i'm little stuck. I have to make 4 services (A,B,C,D). Every service should have his own thread. They should start in sequence and run. If service A starts then can start service B, if service B starts if service C starts then can start service D. I manage to create service and their threads but i dont know how should i create communication between start() and priority() method in PriorityService class. I wan to check if service(thread) A is alive and if it is I want to move to second service from list and so on. Is that possible? Do you have any other ideas how to write service dependency? Any advice is useful. Tnx.

这是我的代码:

import java.util.*;

class CreateThread extends Thread{
    private String thread_name;
    public int numb;
    public CreateThread(String thread_name, int i){
        this.thread_name=thread_name;
        System.out.println("Thread " + thread_name + " has started.");
        i=numb;
    }
    public void run(){
        try{
            Thread t = Thread.currentThread();
            System.out.println(thread_name + " status = " + t.getState());
            System.out.println(thread_name + " status = " + t.isAlive());
            t.join();
        }catch(Exception e){
            System.out.println(e);
        }

    }
}

class PriorityService extends ArrayList<Service> {
    public void priority()
    {
         int i=0;
         while(i<size()){
                System.out.println("evo me"+ get(i).service_name);
                    if(get(i).service_name=="Service A")
                        get(i).StartService(get(i).service_name, get(i).thread_name, i);
                    i++;
            }
    }
 }

public class Service {
    public String service_name;
    public String thread_name;

    public Service(String service_name, String thread_name){
        this.service_name=service_name;
        this.thread_name=thread_name;
    }

    public void StartService(String service_name, String thread_name, int i) {
        System.out.println("Service " + service_name + " has started.");
        Thread t=new Thread(new CreateThread(thread_name, i));
        t.start();
    }

    public void StopService() {}
    public static void main (String[] args){
        PriorityService p_s=new PriorityService();
        Service service_A = new Service("Service A", "Thread A");
        Service service_B = new Service("Service B", "Thread B");
        Service service_C = new Service("Service C", "Thread C");
        Service service_D = new Service("Service D", "Thread D");
        p_s.add(service_A);
        p_s.add(service_B);
        p_s.add(service_C);
        p_s.add(service_D);
        p_s.priority();

        for(Service s: p_s)
            System.out.println(s.service_name);     

    }
}

推荐答案

您应该为此使用闩锁.

您可以为每对线程使用2个闩锁,这应该可以完成您的工作.因此,线程A和线程B会存在一个闩锁,这意味着直到它们两个都启动并运行后它们才能继续进行.对于C和D同样如此.

You can use 2 Latches for one for each pair of threads and that should do your job. So one Latch will be there for Threads A and B, which implies till bothof them are up and running they can't proceed. Similarly for C and D.

链接的示例显示了闩锁,看看吧.

This link has an example showing use of Latch, just take a look.

这篇关于run()方法和其他类方法之间的通信(java线程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆