java多线程的线程数统计
import java.net.*;
import java.util.Set;
import java.io.*;
class JabberClientThread extends Thread {
private Socket socket;
private BufferedReader in;
private PrintWriter out;
private static int counter = 0;
private int id = counter++;
private static int threadcount;
public static int threadCount() {
return threadcount;
}
public static void setThreadcount(int i)
{
threadcount = i;
}
public JabberClientThread(InetAddress addr) {
System.out.println("Making client " + id);
threadcount++; ///线程统计
try {
socket =
new Socket(addr, 8080);
} catch(IOException e) {
// If the creation of the socket fails,
// nothing needs to be cleaned up.
}
try {
in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
// Enable auto-flush:
out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())), true);
start();
} catch(IOException e) {
// The socket should be closed on any
// failures other than the socket
// constructor:
try {
socket.close();
} catch(IOException e2) {}
}
// Otherwise the socket will be closed by
// the run() method of the thread.
}
public void run() {
try {
for(int i = 0; i < 25; i++) {
out.println("Client " + id + ": " + "I Love You");
String str = in.readLine();
System.out.println(str);
}
out.println("END");
} catch(IOException e) {
} finally {
// Always close it:
try {
socket.close();
} catch(IOException e) {}
threadcount--; // Ending this thread
}
}
}
public class MultiJabberClient {
static final int MAX_THREADS = 40;
public static void main(String args[])
throws IOException, InterruptedException {
InetAddress addr =
InetAddress.getByName("192.168.234.138");
JabberClientThread.setThreadcount(0);
while(true) {
if(JabberClientThread.threadCount()
< MAX_THREADS)
{
new JabberClientThread(addr);
Thread.currentThread().sleep(100);
}
else {
break;
}
}
}
} ///:~
这是java编程思想的一个例子,不知道为什么在线程统计的 threadcount++; ///线程统计 这一行threadcount每次并没有+1,各位帮忙看看 --------------------编程问答-------------------- 你确定?为什么我看好像没问题 --------------------编程问答-------------------- 我跑了一下,很正常啊,不知道你是怎么确定ThreadCount没有+1的 --------------------编程问答-------------------- 应该是192.168.234.138这里IP的问题 --------------------编程问答-------------------- 程序原理没有问题 --------------------编程问答--------------------
这段代码显示只要计数到了39就会停下来,但是实际上会一直执行下去,可以跟踪查看一下threadcount的值,永远是1 --------------------编程问答--------------------
if(JabberClientThread.threadCount()
< MAX_THREADS)
{
new JabberClientThread(addr);
Thread.currentThread().sleep(100);
}
我在打印id的后面紧接着打印了ThreadCount的值,
Making client 0
threadcount = 0
Making client 1
threadcount = 1
Making client 2
threadcount = 2
Making client 3
threadcount = 3
Making client 4
threadcount = 4
Making client 5
threadcount = 5
Making client 6
threadcount = 6
Making client 7
threadcount = 7
Making client 8
threadcount = 8
Making client 9
threadcount = 9
Making client 10
threadcount = 10
Making client 11
threadcount = 11
Making client 12
threadcount = 12
Making client 13
threadcount = 13
Making client 14
threadcount = 14
Making client 15
threadcount = 15
Making client 16
threadcount = 16
Making client 17
threadcount = 17
Making client 18
threadcount = 18
Making client 19
threadcount = 19
Making client 20
threadcount = 20
Making client 21
threadcount = 21
Making client 22
threadcount = 22
Making client 23
threadcount = 23
Making client 24
threadcount = 24
Making client 25
threadcount = 25
Making client 26
threadcount = 26
Making client 27
threadcount = 27
Making client 28
threadcount = 28
Making client 29
threadcount = 29
Making client 30
threadcount = 30
Making client 31
threadcount = 31
Making client 32
threadcount = 32
Making client 33
threadcount = 33
Making client 34
threadcount = 34
Making client 35
threadcount = 35
Making client 36
threadcount = 36
Making client 37
threadcount = 37
Making client 38
threadcount = 38
Making client 39
threadcount = 39
确实是到39之后还在执行,但是由于我Socket绑定的是www.baidu.com:80,在39之后停顿了一会儿就打印了很多null出来,你可以自己试试这种情况
补充:Java , Java SE