多線程是較復雜程序設計過程中不可缺少的一部分
本文要求讀者具備一定的Java語言基礎
現在
import java
import
import
public class PortScannerSingleThread {
public static void main(String[] args) {
String host = null; //第一個參數
int beginport =
int endport =
try{
host = args[
beginport = Integer
endport = Integer
if(beginport <=
throw new Exception(
}
}catch(Exception e){
System
System
}
for (int i = beginport; i <= endport; i++) {
try {
Socket s = new Socket(host
System
}catch (UnknownHostException ex) {
System
break;
}catch (IOException ex) {
}
}
}
}
在以上程序中
The port
The port
The port
但是
所以
import java
import
import
public class PortScannerMultiThread {
public static void main(String[] args) {
String host = null;
int beginport =
int endport =
try{
host = args[
beginport = Integer
endport = Integer
if(beginport <=
throw new Exception(
}
}catch(Exception e){
System
System
}
for (int i = beginport; i <= endport; i++) {
PortProcessor pp = new PortProcessor(host
pp
}
}
}
class PortProcessor extends Thread{
String host;
int port;
PortProcessor(String host
this
this
}
public void run(){
try{
Socket s = new Socket(host
System
}catch(UnknownHostException ex){
System
}catch(IOException ioe){
}
}
}
以上程序在for循環結構中創建PortProcessor對象
程序(java PortScannerMultiThread
The port
The port
The port
仔細對第
所以
第一種實現線程池的方法是
第二種方法是
在第
第
import java
import
import
import
import java
import java
import java
public class PortScanner {
private List entries = Collections
int numofthreads;
static int port;
int beginport;
int endport;
InetAddress remote = null;
public boolean isFinished(){
if(port >= endport){
return true;
}else{
return false;
}
}
PortScanner(InetAddress addr
this
this
this
this
}
public void processMethod(){
for(int i =
Thread t = new PortThread(remote
t
}
port = beginport;
while(true){
if(entries
try{
Thread
}catch(InterruptedException ex){
}
continue;
}
synchronized(entries){
if(port > endport) break;
entries
entries
port++;
}
}
}
public static void main(String[] args) {
String host = null;
int beginport =
int endport =
int nThreads =
try{
host = args[
beginport = Integer
endport = Integer
nThreads = Integer
if(beginport <=
throw new Exception(
}
}catch(Exception e){
System
System
}
try{
PortScanner scanner = new PortScanner(InetAddress
scanner
}catch(UnknownHostException ex){
}
}
}
class PortThread extends Thread{
private InetAddress remote;
private List entries;
PortScanner scanner;
PortThread(InetAddress add
this
this
this
}
public void run(){
Integer entry;
while(true){
synchronized(entries){
while(entries
if(scanner
try{
entries
}catch(InterruptedException ex){
}
}
entry = (Integer)entries
}
Socket s = null;
try{
s = new Socket(remote
System
}catch(IOException e){
}finally{
try{
if(s != null) s
}catch(IOException e){
}
}
}
}
}
以上程序需要
第
ThreadPool
import java
public class ThreadPool{
private final int nThreads;
private final PoolWorker[] threads;
private final LinkedList queue;
public ThreadPool(int nThreads){
this
queue = new LinkedList();
threads = new PoolWorker[nThreads];
for (int i=
threads[i] = new PoolWorker();
threads[i]
}
}
public void execute(Runnable r) {
synchronized(queue) {
queue
queue
}
}
private class PoolWorker extends Thread {
public void run() {
Runnable r;
while (true) {
synchronized(queue) {
while (queue
try{
queue
}catch (InterruptedException ignored){
}
}
r = (Runnable) queue
}
try {
r
}
catch (RuntimeException e) {
}
}
}
}
}
在ThreadPool
PortScannerByThreadPool
import java
import
import
public class PortScannerByThreadPool {
public static void main(String[] args) {
String host = null;
int beginport =
int endport =
int nThreads =
try{
host = args[
beginport = Integer
endport = Integer
nThreads = Integer
if(beginport <=
throw new Exception(
}
}catch(Exception e){
System
System
}
ThreadPool tp = new ThreadPool(nThreads);
for(int i = beginport; i <= endport; i++){
Scanner ps = new Scanner(host
tp
}
}
}
class Scanner implements Runnable{
String host;
int port;
Scanner(String host
this
this
}
public void run(){
Socket s = null;
try{
s = new Socket(InetAddress
System
}catch(IOException ex){
}finally{
try{
if(s != null) s
}catch(IOException e){
}
}
}
}
PortScannerByThreadPool是主程序類
讀者可以編譯運行第
在第
synchronized(queue) {
queue
queue
}
和
synchronized(queue) {
while (queue
try{
queue
}catch (InterruptedException ignored){
}
}
r = (Runnable) queue
}
一般拿synchronized用來定義方法或程序塊
wait()和notifyAll()是很重要的
好了
如果讀者對以上的內容有任何疑問
參考資料
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27410.html