java线程与应用程序共用数组问题
ava线程循环不停扫描数组数据,线程启动后,通过应用程序别的方法向这个数组中加数据后,线程中仍读不到应用程序添加进来的数组数据。publicclassTestThread...
ava线程循环不停扫描数组数据,线程启动后,通过应用程序别的方法向这个数组中加数据后,线程中仍读不到应用程序添加进来的数组数据。
public class TestThread extends Thread {
public ArrayList<DataCmdItem> commandArray = new ArrayList<DataCmdItem>();
public boolean isCircle = true;
private Socket socket;
public TCP() {}
public void connect() { }
public void sendDataCmd( byte[] cmd) {
DataCmdItem item = new DataCmdItem();
item.cmd = cmd;
commandArray.add(item);
}
private int count = 0;// 扫描的次数
@Override
public void run() {
super.run();
while (true) {
if (isCircle) {
connect();
count++;
if (socket != null && socket.isConnected()&&!socket.isClosed()) {
if (commandArray.size() > 0) {
try {
out = socket.getOutputStream();
DataCmdItem item = commandArray.get(0); // 每次获取第一条指令
byte[] outByte = item.cmd;
out.write(outByte);
out.flush();
System.out.println("tcp 发送的数据 " + new String(outByte));
commandArray.remove(0); // 执行完后,删除
} catch (IOException e) {e.printStackTrace();}
}
}
}
}
}
}
上面是线程概要代码,下面是调用sendDataCmd向数组commandArray中加数据的。
public static void main(String[] args) {
String strInput2 = "1234567890";
byte[] byBuffer2 = strInput2.getBytes();
for (int a = 0; a < 3; a++) {
sendDataCmd(byBuffer2);
}
} 展开
public class TestThread extends Thread {
public ArrayList<DataCmdItem> commandArray = new ArrayList<DataCmdItem>();
public boolean isCircle = true;
private Socket socket;
public TCP() {}
public void connect() { }
public void sendDataCmd( byte[] cmd) {
DataCmdItem item = new DataCmdItem();
item.cmd = cmd;
commandArray.add(item);
}
private int count = 0;// 扫描的次数
@Override
public void run() {
super.run();
while (true) {
if (isCircle) {
connect();
count++;
if (socket != null && socket.isConnected()&&!socket.isClosed()) {
if (commandArray.size() > 0) {
try {
out = socket.getOutputStream();
DataCmdItem item = commandArray.get(0); // 每次获取第一条指令
byte[] outByte = item.cmd;
out.write(outByte);
out.flush();
System.out.println("tcp 发送的数据 " + new String(outByte));
commandArray.remove(0); // 执行完后,删除
} catch (IOException e) {e.printStackTrace();}
}
}
}
}
}
}
上面是线程概要代码,下面是调用sendDataCmd向数组commandArray中加数据的。
public static void main(String[] args) {
String strInput2 = "1234567890";
byte[] byBuffer2 = strInput2.getBytes();
for (int a = 0; a < 3; a++) {
sendDataCmd(byBuffer2);
}
} 展开
2015-07-22
展开全部
SQL code?
select * from (
(SELECT t.*, f.name FROM threads t, forums f where f.fid=t.fid id=10000)
union all
(SELECT t.*, f.name FROM threads t, forums f ORDER BY t.dateline DESC LIMIT 0, 10)
) t
order by id=10000 desc,datelineDESC LIMIT 0, 10
select * from (
(SELECT t.*, f.name FROM threads t, forums f where f.fid=t.fid id=10000)
union all
(SELECT t.*, f.name FROM threads t, forums f ORDER BY t.dateline DESC LIMIT 0, 10)
) t
order by id=10000 desc,datelineDESC LIMIT 0, 10
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询