简体   繁体   中英

Java (Raspberry pi) Thread

I am a student who is studying java.(Especially Raspberry pi) I have a question this multuthread. It can be compiled. But it doesn't work in my kit. If you don't mind guys, could you check my code and help me? Thanks...

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.File;
import java.io.FileWriter;

public class RcvThread2 implements Runnable{
    private static final int sizeBuf = 50;
    private Socket clientSocket;
    private Logger logger;
    private SocketAddress clientAddress;

public RcvThread2(Socket clntSock, SocketAddress clientAddress, Logger logger) {
    this.clientSocket = clntSock;
    this.logger = logger;
    this.clientAddress = clientAddress;
    }

static class CloseExtends extends Thread {
    static final String GPIO_OUT = "out";
    static final String GPIO_ON = "1";
    static final String GPIO_OFF = "0";
    static final String[] GpioChannels = {"18"};

public static void main(String[] args) {
    FileWriter[] commandChannels;

    try {
        FileWriter unexportFile = new FileWriter("sys/class/gpio/unexport");
    FileWriter exportFile = new FileWriter("sys/class/gpio/gpio/export");

for(String gpioChannel : GpioChannels) {
    System.out.println(gpioChannel);

    File exportFileCheck =
        new File("sys/class/gpio/gpio" +gpioChannel);
        if(exportFileCheck.exists()) {
            unexportFile.write(gpioChannel);
            exportFile.flush();
        }
        exportFile.write(gpioChannel);
        exportFile.flush();

        FileWriter directionFile = new FileWriter("/sys/class/gpio/gpio" + gpioChannel + "/direction");

        directionFile.write(GPIO_OUT);
        directionFile.flush();
    }
    FileWriter commandChannel = new FileWriter("sys/class/gpio/gpio" + GpioChannels[0] + "/value");

    int period = 20;
    int repeatLoop = 25;
    int counter;

    while(true) {
        for(counter = 0; counter < repeatLoop; counter++) {
            commandChannel.write(GPIO_ON);
            commandChannel.flush();
            java.lang.Thread.sleep(2, 20000);

            commandChannel.write(GPIO_OFF);
            commandChannel.flush();
            java.lang.Thread.sleep(period);
        }
        break;
    }
} catch(Exception exception) {
    exception.printStackTrace();
    }
}
}

public void main(){
try {
    InputStream ins = clientSocket.getInputStream();
    OutputStream outs = clientSocket.getOutputStream();

    int rcvBufSize;
    byte[] rcvBuf = new byte[sizeBuf];
    while ((rcvBufSize = ins.read(rcvBuf)) != -1) {

    String rcvData = new String(rcvBuf, 0, rcvBufSize, "UTF-8");

    if(rcvData.compareTo("MotorLock") == 0) {
        CloseExtends te = new CloseExtends();
        te.start();
    }

    if(rcvData.compareTo("MotorOpen") == 0) {
    }

    logger.info("Received data :" + rcvData + " (" + clientAddress + ")");
    outs.write(rcvBuf, 0, rcvBufSize);
    }

    logger.info(clientSocket.getRemoteSocketAddress() + "Closed");
    } catch (IOException ex) {
        logger.log(Level.WARNING, "Exception in RcvThread", ex);
    }finally {
        try{
            clientSocket.close();
                System.out.println("Disconnected! Client IP :" + clientAddress);
                } catch (IOException e) {}
            }
        }
    }

The lower main method never gets called. If you run your program it will execute the public static void main(String[] args) { method.

I think this is the method you want to run in the second thread?!

If you declare and run your new thread using

CloseExtends te = new CloseExtends(); te.start(); it will run the threads public void run() { method.

So if I understand your intention correctly you should change the name of the main method in the CloseExtends class to the threads run method and change the signature of the lower main method to the java programs main method public static void main(String[] args) { .

I would not name any other method "main" if it is not really a main method.

You can see an example of creating a new thread with the Runnable interface here: https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM