????4.   ??????
????????????5????:
????1?? ??? (Thread t = new Thread())
????2?? ?? (t.start())
????3?? ????
????4?? ????
????????block???????????: ???IO?????? sleep()?? ??????????????
????5??????
????5.?????????????????????: ????????????????????synchronized
?????????????:
??????????????????synchronized?????????????????????????????????????synchronized????????????????????????????????????synchronized?????????????????????????????????????????????????????? ??????????????????????ж???????
????6.synchronized?????
?????????????????????????????飬??????????????????synchronized??????????????????synchronized????顣???£?????this??????????
public void function()
{
doSomething();
//???·???????????
synchronized (this)
{
doCriticalStuff();
moreCriticalStuff();
}
doSomeOtherThing();
}
????7.???????????Socket????????:
??????????????????:
/**
* @author notifier
* @create 2010-9-25 ????10:12:10
* @version 1.0
*/
public class DailyAdviceClient
{
public static void main(String[] args)
{
DailyAdviceClient client = new DailyAdviceClient();
client.receiveMsg();
}
public void receiveMsg()
{
try
{
// 1.????Socket????????Server??IP??????????
Socket socket = new Socket("127.0.0.1"?? 4242);
// 2.????InputStreamReader????????socket??????
InputStreamReader stream = new InputStreamReader(socket
.getInputStream());
// 3.???BufferedReader??????????
BufferedReader br = new BufferedReader(stream);
// 4.????????
String line = null;
while ((line = br.readLine()) != null)
{
System.out.println("Today's advice is: " + line);
}
// 5. ?????????BufferedReader
br.close();
} catch (UnknownHostException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}