java并发编程(三)之 传统线程思想与细节

一、构造方法与匿名内部类

/**
 * new Thread(){}:在构造方法 new Thread() 后面加上{},表示创建一个匿名内部类的对象.
 * (父类:Thread;子类:内部类),在{} 实现run()后,调用时会覆盖父类方法.
 * new Thread(Runnable runnable): 带参构造.调用时则会执行 runnable 的run().【参见Thread类源码】
 * 
 * new Thread(Runnable Runnable){}:由于子类会覆盖父类方法,则是调用子类的run().
 * @author Dason
 *
 */
public class TraditionalThread {

	public static void main(String[] args) {
	
		new Thread(
				new Runnable(){
					public void run() {
						while(true){
							try {
								Thread.sleep(500);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
							System.out.println("runnable :" + Thread.currentThread().getName());

						}							
					}
				}
		){
			public void run() {
				while(true){
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println("thread :" + Thread.currentThread().getName());

				}	
			}
		}.start();

	}

}

二、循环炸弹

package com.dason.first;

/**
 * second
 */
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
/**
 * 模拟连环炸弹:2s后炸一个后,等待4s后炸一个,再等待2s后炸一个,等待4s后炸一个...如此循环
 * @author Dason
 *
 */
public class TraditionalTimerTest {

	private static int count = 0;
	public static void main(String[] args) {
		class MyTimerTask extends TimerTask{
			
			@Override
			public void run() {
				count = (count+1)%2;
				System.out.println("bombing!");
				new Timer().schedule(new MyTimerTask(),2000+2000*count);
			}
		}
		
		new Timer().schedule(new MyTimerTask(), 2000);
		
		while(true){
			System.out.println(new Date().getSeconds());
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}

三、线程互斥与同步方法的锁

package com.dason.first;
/**同一个类的同步方法是互斥的
 * 同步方法的锁默认为当前对象,而不是类锁
 * @author Dason
 * 
 * 打印结果
 *aaaaaccccccccaaa
 *c
 *bbbbbbb
 *aaaaaaaaccccccccc
 *bbbbbbb
 *bbbbbbb
 *ccccccccc
 *aaaaaaaa
 *cccccbbbbbbb
 */
public class TraditionalThreadSynchronized {

	//静态方法中不能创建内部类的实例
	//在外部类的非静态方法中,因为有隐含的外部类引用this,所以可以直接创建非静态内部类.
	//在外部类的静态方法中,因为没有this,所以必须先获得外部类引用,然后创建非静态内部类.
	public static void main(String[] args) {
//		final Outputer outputer = new Outputer();
		new TraditionalThreadSynchronized().init();
	}
	
	
	private void init(){
		final Outputer outputer = new Outputer();
		new Thread(new Runnable(){
			@Override
			public void run() {
				while(true){
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					outputer.output("aaaaaaaa");
				}
				
			}
		}).start();
		
		new Thread(new Runnable(){
			@Override
			public void run() {
				while(true){
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					outputer.output2("bbbbbbb");
				}
				
			}
		}).start();
		
		new Thread(new Runnable(){
			@Override
			public void run() {
				while(true){
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					outputer.output3("ccccccccc");
				}
				
			}
		}).start();
		
	}

	 class Outputer{
		
		public void output(String name){
			int len = name.length();
			synchronized (this) 
			{
				for(int i=0;i<len;i++){
					System.out.print(name.charAt(i));
				}
				System.out.println();
			}
		}
		
		public synchronized void output2(String name){
			int len = name.length();
			for(int i=0;i<len;i++){
					System.out.print(name.charAt(i));
			}
			System.out.println();	
		}
		
		public void output3(String name){
			int len = name.length();
			synchronized (Outputer.class) 
			{
				for(int i=0;i<len;i++){
					System.out.print(name.charAt(i));
				}
				System.out.println();
			}
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值