1
votes

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.

Here is my code:

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
Reading through - i=numb in the constructor of CreateThread seems wrong, shouldn't it be the other way around?selig
Calling join on the current thread won't do anything.selig
that peace of code stayed there because I was trying to send int i from while loop to CreateThread class and then if that thread is alive do i++ witch becomes numb in run() method and send that to while loop in priority() method. I dont know if that is good idea because i'm new in programming.njamanjam
@seling with join method i'm trying to run all threads without stooping. As i said i dont know if anything from this is good idea :S I'm confused...njamanjam
Calling Thread.currentThread().join(); will hang until the thread is interrupted which is probably not what you want.Gray

2 Answers

0
votes

If you are creating different threads for each services, then you cannot control the execution is the threads (e.g. by setting its priority, etc). Priority is just an indicator for OS, but can not be guarantee that threads with higher priority runs first.

Only way you can do this by inter-thread communication using wait, notify, join, etc.

But I think in case you case the solution could be, create separate thread for one combination of Service A, B, C & D.

0
votes

You should use Latches for this.

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.