這樣
為了說明這第二種情況
importjavax
importjava
importjava
publicclasscount
{
publicstaticvoid
main(stringargs)
{
finalstringurlstring=args;
finalstringmessage=args;
threadthread
{
publicvoidrun()
{
try
{
urlurl=newurl(urlstring);
urlconnectionconnection=
url
inputstreamreaderisr=new
inputstreamreader(
connection
bufferedreaderreader=
newbufferedreader(isr);
intcount=
while(reader
{
count++;
}
system
+count);
reader
}catch(malformedurlexceptione)
{
system
+urlstring);
}catch(ioexceptione)
{
system
(
}
}
};
thread
runnablerunnable=newrunnable()
{
publicvoidrun()
{
while(true)
{
system
try
{
thread
}catch(interruptedexceptione)
{
}
}
}
};
threadthread
thread
try{
system
(
system
}catch(ioexceptione)
{
system
}
system
}
}
因為有多種方式來處理線程
[
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27734.html