Przekazanie danych do innego wątku który jest klientem UDP

0

Wątek pierwszy ma na celu co jakiś czas pobierać obciążenie CPU oraz ilość wolnej pamięci RAM. To mam napisane teraz nie wiem jednak jak przekazać te dane do drugiego wątku który będzie te dane przekazywał protokołem UDP pod wyznaczony serwer UDP. Powinienem stworzyć oddzielny wątek w głównym main czy wątek w wątku?

MobileSystemMonitor.class

package pl.garrymoveout

public class MobileSystemMonitor {
    public static void main(String[] args) {
        String serverIp = args[0];
        String serverPort = args[1];
        String refreshRate = args[2];

        int refreshMilisec = Integer.parseInt(refreshRate);

        RefreshStatusThread T1 = new RefreshStatusThread(serverIp, serverPort, refreshMilisec);
        T1.start();
    }
}

RefreshStatusThread.class

package pl.garrymoveout;

import org.hyperic.sigar.*;

public class RefreshStatusThread extends Thread{
    private Thread thread;

    private String ipAddres;
    private String ipAddresPort;
    private int refreshRateMs;

    Sigar sigar;
    Mem memory;
    CpuPerc cpu;
    int cpuUsed;
    int ramFree;

    RefreshStatusThread(String ipAddr, String ipPort, int refInMiliSec){
        System.out.println("Creating thread");
        ipAddres = ipAddr;
        ipAddresPort = ipPort;
        refreshRateMs = refInMiliSec;

        sigar = new Sigar();
    }

    public void run(){
        while(!Thread.currentThread().isInterrupted()) {
            try {
                memory = sigar.getMem();
                cpu = sigar.getCpuPerc();
            } catch (SigarException e) {
                e.printStackTrace();
            }
            cpuUsed = (int) (cpu.getCombined()*100);
            ramFree = (int) (memory.getFreePercent());

            System.out.println("CPU usage:" + cpuUsed + "% Free memory:" + ramFree + "%");

            try {
                Thread.sleep(refreshRateMs);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                e.printStackTrace();
            }
        }
        System.out.println("Exit thread");
    }

    public void start(){
        System.out.println("Thread start");
        thread = new Thread(this);
        thread.start();
    }
}
 
0

Dawaj to do zmiennej statyczna i pobieraj ;)

1 użytkowników online, w tym zalogowanych: 0, gości: 1