gpt4 book ai didi

Java(树莓派)线程

转载 作者:行者123 更新时间:2023-12-01 10:50:43 25 4
gpt4 key购买 nike

我是一名正在学习java的学生。(特别是Raspberry pi)我有一个关于多线程的问题。是可以编译的。但它在我的套件中不起作用。如果你们不介意的话,可以检查一下我的代码并帮助我吗?谢谢...

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) {}
}
}
}

最佳答案

下面的 main 方法永远不会被调用。如果运行程序,它将执行 public static void main(String[] args) { 方法。

我认为这是您想要在第二个线程中运行的方法?!

如果您使用

声明并运行新线程

CloseExtends te = new CloseExtends();
te.start();
它将运行线程 public void run() { 方法。

因此,如果我正确理解您的意图,您应该将 CloseExtends 类中的 main 方法的名称更改为线程 run 方法,并将较低的 main 方法的签名更改为 java 程序的 main 方法 public static void main (字符串[]参数){

如果任何其他方法不是真正的主方法,我不会将其命名为“main”。

您可以在此处查看使用 Runnable 接口(interface)创建新线程的示例:https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html

关于Java(树莓派)线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33933813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com