简体   繁体   English

Java(Raspberry pi)线程

[英]Java (Raspberry pi) Thread

I am a student who is studying java.(Especially Raspberry pi) I have a question this multuthread. 我是一名正在学习Java的学生。(特别是Raspberry pi)我对这个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. 较低的main方法永远不会被调用。 If you run your program it will execute the public static void main(String[] args) { method. 如果您运行程序,它将执行public static void main(String[] args) {方法。

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. 它将运行线程public void run() {方法。

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) { . 因此,如果我正确理解了您的意图,则应将CloseExtends类中的main方法的名称更改为thread run方法,并将较低main方法的签名更改为java程序main方法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 您可以在此处看到使用Runnable接口创建新线程的示例: https : //docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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