疯子在思考之java 线程的那点事儿
很长时间没写博客了,最近事情比较多
之前在文章中提到过tomcat 的main函数在哪?被很多朋友拍砖了
今天继续就这话题展开,先了解几个线程有关的概念
1、多线程 multithread
为什么要用多线程?就是让cpu别太闲,有空就要干活,提高效率。
2、线程池 threadpool
为什么要用线程池,所有跟池相关的,如connectionPool(数据库连接池),ajax request请求对 象池、线程池等都是为了减少对象new所带来的开销.
3、线程安全 thread safe
所谓的线程安全就是指多线程的运行结果与单线程的运行结果一致,java 通过synchronized和threadlocal等解决线程安全问题。
线程不安全是由于对共享资源(如static 变量 单例成员变量 文件 数据库 缓存等)的同步写操作而引起的。
tomcat 的main在这里
它会启动一个serviceSoket监听客户端socket请求.
org.apache.catalina.startup.Bootstrap.java
具体功能有一本书《深入剖析Tomcat》有兴趣的朋友可以了解一下。
我们先用最简单的socket 看看他的工作原理
服务器 socket代码
package service;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;public class ServiceSocketDemo {/** * @author zlz * * @time 2013-7-26下午5:00:04 * @param args * @throws IOException */public static void main(String[] args) throws IOException { //服务器端启动后会不停地监听该端口请求ServerSocket serverSocket = new ServerSocket(1999);while (true) { //接受请求Socket socket = serverSocket.accept();BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));String msg = null;if ((msg = br.readLine()) != null) {System.out.println("服务器端收到—->" + msg);}PrintWriter pw = new PrintWriter(socket.getOutputStream(), true); //处理后并输出到客户端pw.println("hi");socket.close();}}}package client;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.Socket;import java.net.UnknownHostException;public class ClientSocketDemo {/** * @author zlz * * @time 2013-7-26下午5:03:52 * @param args * @throws IOException * @throws UnknownHostException */public static void main(String[] args) throws UnknownHostException,IOException { //请求127.0.0.1:1999端口Socket socket = new Socket("127.0.0.1", 1999);PrintWriter pw = new PrintWriter(socket.getOutputStream(), true); //发送hello 并等待服务器端处理pw.println("helllo"); BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));String msg = null;if ((msg = br.readLine()) != null) {System.out.println("客户端收到—->" + msg);}}}//线程安全的 getInstance一般为单例的方法,但jdk里每次都会new 一个变量出现。Calendar c=Calendar.getInstance(); /** * Gets a calendar using the default time zone and locale. The * <code>Calendar</code> returned is based on the current time * in the default time zone with the default locale. * * @return a Calendar. */ public static Calendar getInstance() { Calendar cal = createCalendar(TimeZone.getDefaultRef(), Locale.getDefault());cal.sharedZone = true;return cal; } private static Calendar createCalendar(TimeZone zone, Locale aLocale) {// If the specified locale is a Thai locale, returns a BuddhistCalendar// instance.if ("th".equals(aLocale.getLanguage()) && ("TH".equals(aLocale.getCountry()))) { return new sun.util.BuddhistCalendar(zone, aLocale);} else if ("JP".equals(aLocale.getVariant()) && "JP".equals(aLocale.getCountry()) && "ja".equals(aLocale.getLanguage())) { return new JapaneseImperialCalendar(zone, aLocale);} // else create the default calendar return new GregorianCalendar(zone, aLocale); }