java多线程

 创建线程的两种传统方式
  1).在Thread子类覆盖的run方法中编写运行代码

  1. Thread thread1=new Thread(){ 
  2.             @Override 
  3.             publicvoid run() { 
  4.                 while(true){ 
  5.                     try
  6.                         Thread.sleep(500); 
  7.                     } catch (InterruptedException e) { 
  8.                         e.printStackTrace(); 
  9.                     } 
  10.                     System.out.println("0 : "+Thread.currentThread      ().getName()); 
  11.                 } 
  12.             } 
  13.         }; 
  14.          
  15.         thread1.start(); 
Thread thread1=new Thread(){
			@Override
			public void run() {
				while(true){
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println("0 : "+Thread.currentThread		().getName());
				}
			}
		};
		
		thread1.start();

2).在传递给Thread对象的Runnable对象的run方法中编写代码

  1. Thread thread2=new Thread(new Runnable() { 
  2.              
  3.             @Override 
  4.             publicvoid run() { 
  5.                  
  6.                 while(true){ 
  7.                     try
  8.                         Thread.sleep(500); 
  9.                     } catch (InterruptedException e) { 
  10.                         e.printStackTrace(); 
  11.                     } 
  12.                     System.out.println("1 : "+Thread.currentThread().getName()); 
  13.                 } 
  14.             } 
  15.         }); 
  16.         thread2.start(); 
Thread thread2=new Thread(new Runnable() {
			
			@Override
			public void run() {
				
				while(true){
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println("1 : "+Thread.currentThread().getName());
				}
			}
		});
		thread2.start();

总结:查看Thread类的run()方法的源代码,可以看到其实这两行方式都是在调用Thread对象的run方      法,如果Thread类的run方法没有被覆盖,并且为该Thread对象设置了一个Runnable对象,该run       方法会调用Runnable对象的run方法。
   如果在Thread子类覆盖的run方法中编写了运行代码,也为Thread子类对象传递了一个Runnable对象    ,那么,线程运行时的执行代码是子类的Run方法的代码。   

  1. new Thread(new Runnable() { 
  2.          
  3.         @Override 
  4.         publicvoid run() { 
  5.             while(true){ 
  6.                 try
  7.                     Thread.sleep(500); 
  8.                 } catch (InterruptedException e) { 
  9.                     e.printStackTrace(); 
  10.                 } 
  11.                 System.out.println("runnable : "+Thread.currentThread().getName()); 
  12.             } 
  13.              
  14.         } 
  15.     }){ 
  16.         publicvoid run() { 
  17.             while(true){ 
  18.                 try
  19.                     Thread.sleep(500); 
  20.                 } catch (InterruptedException e) { 
  21.                     e.printStackTrace(); 
  22.                 } 
  23.                 System.out.println("thread : "+Thread.currentThread().getName()); 
  24.             } 
  25.         }; 
  26.     }.start(); 
	new Thread(new Runnable() {
			
			@Override
			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();

输出结果为thread : Thread-2
2.定时器的应用    1)Timer类    2)TimerTask类

  1. new Timer().schedule(new TimerTask() { 
  2.              
  3.             @Override 
  4.             publicvoid run() { 
  5.                 System.out.println("bombing"); 
  6.                  
  7.             } 
  8.         }, 10000,3000); 
new Timer().schedule(new TimerTask() {
			
			@Override
			public void run() {
				System.out.println("bombing");
				
			}
		}, 10000,3000);

3.线程的同步互斥与通信
  1)使用synchronized代码块及其原理 public  void output(String name){ synchronized (this) { for (int i = 0; i < name.length(); i++) { System.out.print(name.charAt(i)+" "); } System.out.println(); } }   2)使用synchronized方法 class OutPuter{ public synchronizedvoid output(String name){ for (int i = 0; i < name.length(); i++) { System.out.print(name.charAt(i)+" "); } System.out.println(); } }
面试题:子线程循环10次,接着主线程循环100次,接着又回到子线程循环10次,接着再回到主线程又         循环100次,如此循环50次,请写出程序。

  1. publicclass TraditionalThreadCommunication { 
  2.  
  3.  
  4.     /**
  5.      * @param args
  6.      */ 
  7.     publicstaticvoid main(String[] args) { 
  8.          
  9.         final Business business=new Business(); 
  10.          
  11.         //子线程 
  12.         new Thread(new Runnable() { 
  13.              
  14.             @Override 
  15.             publicvoid run() { 
  16.                 for (int i = 1; i <=50; i++) { 
  17.                     business.sub(i); 
  18.                 } 
  19.             } 
  20.         }).start(); 
  21.          
  22.          
  23.         //主线程 
  24.         for (int i = 1; i <=50; i++) { 
  25.             business.main(i); 
  26.         } 
  27.  
  28.  
  29.     } 
  30.  
  31.  
  32.  
  33.  
  34. class Business{ 
  35.      
  36.     privateboolean bShouldSub=true
  37.      
  38.     publicsynchronizedvoid sub(int i){ 
  39.         while(!bShouldSub){ 
  40.             try
  41.                 this.wait();//等待 
  42.             } catch (InterruptedException e) { 
  43.                 e.printStackTrace(); 
  44.             } 
  45.         } 
  46.         for (int j = 1; j <= 10; j++) { 
  47.             System.out.println("sub thread sequence of " + j 
  48.                         + " , loop of " + i); 
  49.         } 
  50.          
  51.         bShouldSub=false
  52.         this.notify();//唤醒(防止死锁) 
  53.          
  54.     } 
  55.     publicsynchronizedvoid main(int i){ 
  56.         while(bShouldSub){ 
  57.             try
  58.                 this.wait(); 
  59.             } catch (InterruptedException e) { 
  60.                 e.printStackTrace(); 
  61.             } 
  62.         } 
  63.         for (int j = 1; j <= 20; j++) { 
  64.             System.out.println("main thread sequence of " + j 
  65.                     + " , loop of " + i); 
  66.         } 
  67.          
  68.         bShouldSub=true
  69.         this.notify(); 
  70.     } 
  71.      
public class TraditionalThreadCommunication {


	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		final Business business=new Business();
		
	 	//子线程
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				for (int i = 1; i <=50; i++) {
					business.sub(i);
			    }
			}
		}).start();
		
		
		//主线程
		for (int i = 1; i <=50; i++) {
			business.main(i);
	    }


	}


}


class Business{
	
	private boolean bShouldSub=true;
	
	public synchronized void sub(int i){
		while(!bShouldSub){
		    try {
				this.wait();//等待
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		for (int j = 1; j <= 10; j++) {
			System.out.println("sub thread sequence of " + j
						+ " , loop of " + i);
		}
		
		bShouldSub=false;
		this.notify();//唤醒(防止死锁)
		
	}
	public synchronized void main(int i){
		while(bShouldSub){
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		for (int j = 1; j <= 20; j++) {
			System.out.println("main thread sequence of " + j
					+ " , loop of " + i);
		}
		
		bShouldSub=true;
		this.notify();
	}
	
}

   (总结:要用到共同数据(包括同步锁)的若干个方法,应该归在同一个类身上,这种设计正好体现了高类聚和程序的健壮性。)

posted @ 2012-12-22 22:08  tse johnson  阅读(186)  评论(0编辑  收藏  举报