首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

保守线程技术回顾

2013-08-16 
传统线程技术回顾/** * TraditionalThread.java * cn.com.songjy.test.socket.thread * Function: TODO* *

传统线程技术回顾

/** * TraditionalThread.java * cn.com.songjy.test.socket.thread * Function: TODO  * *   version    date      author * ────────────────────────────────── *   1.0 2013-8-15    songjy * * Copyright (c) 2013, TNT All Rights Reserved. */package cn.com.songjy.test.socket.thread;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;/** * ClassName:TraditionalThread *  * @author songjy * @version 1.0 * @since v1.0 * @Date 2013-8-15 下午8:51:31 */public class TraditionalThread {private static Log log = LogFactory.getLog(TraditionalThread.class);public static void main(String[] args) {/*********线程一*********/Thread thread1 = new Thread() {@Overridepublic void run() {while (true) {try {Thread.sleep(500);} catch (InterruptedException e) {log.error(e.getMessage(), e);}log.info("11:" + Thread.currentThread().getName());log.info("12:" + this.getName());// 不推荐}}};thread1.start();/*********线程二*********/Thread thread2 = new Thread(new Runnable() {@Overridepublic void run() {while (true) {try {Thread.sleep(500);} catch (InterruptedException e) {log.error(e.getMessage(), e);}log.info("21:" + Thread.currentThread().getName());// log.info("22:" + this.getName());// 此处不能使用this}}});thread2.start();/*********线程三*********///猜猜看本线程打印出的是【31Runnable:+线程名】还是【31+线程名】new Thread(new Runnable(){@Overridepublic void run() {while (true) {try {Thread.sleep(500);} catch (InterruptedException e) {log.error(e.getMessage(), e);}log.info("31Runnable:" + Thread.currentThread().getName());}}}) {@Overridepublic void run() {//运行的是此方法,因为子类覆盖(重写)了父类方法,运行的将是子类方法,这是面向对象的一个重要特性while (true) {try {Thread.sleep(500);} catch (InterruptedException e) {log.error(e.getMessage(), e);}log.info("31:" + Thread.currentThread().getName());}}}.start();}//多线程不一定执行效率快,好比一个人做馒头给3个桌面上人吃,在一个桌面上做自然比给每个人一个桌面上做一个又跑到另一个桌面上快//多线程下载是抢资源,服务器为一个线程分配20k,我多开线程下载,自然分配的带宽就多了}


引自
http://down.51cto.com/data/443405

热点排行