我使用带有多个线程的Java编程服务器,很早就买了我遇到了一个非常奇怪的故障。
这是我的代码:
package net.twh;
import java.net.DatagramSocket;
import java.util.Scanner;
public class Server {
public static EntryThread entry;
public static ServerPhases phase;
public static DatagramSocket serverSocket;
public static boolean stopProgram;
public static Scanner keyboard;
public static void main(String[] args) {
Configuration.port = 17550;
keyboard = new Scanner(System.in);
entry = new EntryThread();
entry.run();
while (!stopProgram)
{
String line = "";
System.out.printf("> ");
line = keyboard.next();
System.out.printf(line);
if (line == "close")
{
entry.stop = true;
stopProgram = true;
}
}
}
}
这是我的主题:
package net.twh;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class EntryThread extends Thread {
public boolean stop;
public EntryThread() {
stop = false;
}
public void run() {
Runtime.getRuntime().addShutdownHook(new Thread(){public void run(){ // AUTOCLOSE NE PAS SUPPRIMER !!!!!
try {
Server.serverSocket.close();
System.out.println("The server is shut down!");
} catch (Exception e) { /* failed */ }
}});
try {
Server.serverSocket = new DatagramSocket(Configuration.port);
byte[] receiveData = new byte[8];
System.out.printf("Listening on udp:%s:%d%n",
InetAddress.getLocalHost().getHostAddress(), Configuration.port);
DatagramPacket receivePacket = new DatagramPacket(receiveData,
receiveData.length);
while(!stop) {
Server.serverSocket.receive(receivePacket);
byte[] data = receivePacket.getData();
System.out.println("RECEIVED: " + new String(data));
InetAddress IPAddress = receivePacket.getAddress();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
接收数据包时的消息有效且扫描仪有效,但"> "我输入的字符串不显示。
答案 0 :(得分:3)
您没有启动该线程,您只是按顺序执行该线程的run()
方法。启动线程的正确方法是调用entry.start()
而不是entry.run()
。
另见:
https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html
答案 1 :(得分:0)
<batch:job id="GhanshyamESCatalogUpdater">
<batch:step id="GhanshyamCatalogUpdater2" >
<batch:partition step="slave" partitioner="rangePartitioner">
<batch:handler grid-size="15" task-executor="taskExecutor" />
</batch:partition>
</batch:step>
<batch:listeners>
<batch:listener ref="jobFailureListener"/>
</batch:listeners>
</batch:job>
<bean id="rangePartitioner" class="org.springframework.batch.core.partition.support.MultiResourcePartitioner" scope="step">
<property name="resources" value="file:#{jobParameters['job.partitionDir']}/x*">
</property>
</bean>
<step id="slave" xmlns="http://www.springframework.org/schema/batch">
<tasklet>
<chunk reader="gsbmyntraXmlReader" writer="gsbmyntraESWriter" commit-interval="1000" />
</tasklet>
</step>
的流是<bean id="taskExecutor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="100" />
<property name="allowCoreThreadTimeOut" value="true" />
<property name="WaitForTasksToCompleteOnShutdown" value="true" />
</bean>
,仅在换行符时刷新其输出(请参阅this answer)。但您可以明确调用System.out
或打印换行符(在您的示例中甚至会更好看):
PrintStream
由于我不明白为什么你使用flush()
而没有任何格式化,我用适当的方法替换它。
BTW:JMSilla是对的,你的代码没有同时执行任何操作,但这不是问题。
答案 2 :(得分:0)
现在你调用run方法而不是start(),Thread中的run()方法是start在运行时创建新线程并不创建任何线程,只需像普通方法调用一样在当前线程中执行。
entry = new EntryThread();
entry.run();
使用
entry.start()