기본 코드-다 중 스 레 드


import java.util.Date;

/*******************************************************************************
 *               :  Runnable     Thread          ,        , 
 *       ,       ;    Runnable         
 * 
 */
public class ThreadJava {

	static void Runner()//                  run       start     
	{//                 。
		Runner1 r = new Runner1();
		Thread t = new Thread(r);
		t.start();//         (  。    cpu  )

		Runner2 r1 = new Runner2();
		Thread t1 = new Thread(r1);
		t1.start();//         

		Runner3 g1 = new Runner3();
		g1.start();//         

		for (int i = 0; i < 100; i++) {
			System.out.println("   :---------" + i);
		}
	}

	static void Runnerr() {//           
		MyThread thread = new MyThread();
		thread.start();
		try {// sleep         ,   Thread    
			Thread.sleep(10000);// sleep   10   
		} catch (InterruptedException e) {
		}
		thread.interrupt();//      stop()       ,    
	}

	static void Runnerre() {//            
		MyThread2 t1 = new MyThread2("  ");
		t1.start();//     
		try {
			t1.join();//           。  ,        ,      
		} catch (InterruptedException e) {
		}

		for (int i = 1; i <= 10; i++) {
			System.out.println("   ");
		}
	}

	static void Runnerrewe()//     
	{
	Thread t1 = new Thread(new Runner1());
	Thread t2 = new Thread(new Runner2());
	t1.setPriority(Thread.NORM_PRIORITY + 3);//     ,          
	t1.start();
	t2.start();
	}
	
	static void Runnerrew() {//        10            
		MyThread3 t1 = new MyThread3("   ");
	    MyThread3 t2 = new MyThread3("   ");
	    t1.start(); t2.start();
	}
	
	public static void main(String[] args) {

		Runner();//            
		Runnerr();//           
		Runnerre();//            
		
		Runnerrewe();//     
		Runnerrew();//3   ,        10            
	}

}

//   Runnable   
class Runner1 implements Runnable {
	public void run() {//     run()  
		for (int i = 0; i < 100; i++) {
			System.out.println("      -- :" + i);
		}
	}
}

//   Runnable   
class Runner2 implements Runnable {
	public void run() {//     run()  
		for (int i = 0; i < 100; i++) {
			System.out.println("      -- :" + i);
		}
	}
}

//   Thread   
class Runner3 extends Thread {
	public void run() {//     run()  
		for (int i = 0; i < 100; i++) {
			System.out.println("      -- :" + i);
		}
	}
}


//   sleep       
class MyThread extends Thread {
	boolean flag = true;

	public void run() {//     throw    ,  run      。         
		while (flag) {
			System.out.println("===" + new Date() + "===");
			try {//    try catch    sleep  
				sleep(1000);
			} catch (InterruptedException e) {
				return;
			}
		}
	}
}

//   join     。       1   
class MyThread2 extends Thread {
	MyThread2(String s) {
		super(s);//    Thread    ,       
	}
	public void run() {
		for (int i = 1; i <= 10; i++) {
			System.out.println("   " + getName());
			try {
				sleep(1000);
			} catch (InterruptedException e) {
				return;
			}
		}
	}
}
//  yield       
class MyThread3 extends Thread {
  MyThread3(String s){super(s);}
  public void run(){
    for(int i =1;i<=100;i++){
      System.out.println(getName()+": "+i);
      if(i%10==0){// i   10     
        yield();//  cpu  ,       
      }
    }
  }
}

생산자 와 소비자 문제

/*synchronized        
 wait            ,        ,     
 notify/notifyAll    、      */

public class ProducerConsumer {
	public static void main(String[] args) {
		SyncStack ss = new SyncStack();
		Producer p = new Producer(ss);
		Consumer c = new Consumer(ss);
		new Thread(p).start();
		new Thread(p).start();
		new Thread(p).start();
		new Thread(c).start();
	}
}

class WoTou {
	int id;

	WoTou(int id) {
		this.id = id;
	}

	public String toString() {
		return "WoTou : " + id;
	}
}

class SyncStack {
	int index = 0;
	WoTou[] arrWT = new WoTou[6];

	public synchronized void push(WoTou wt) {
		while (index == arrWT.length) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		this.notifyAll();
		arrWT[index++] = wt;
	}

	public synchronized WoTou pop() {
		while (index == 0) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		this.notifyAll();
		return arrWT[--index];
	}
}

class Producer implements Runnable {
	SyncStack ss = null;

	Producer(SyncStack ss) {
		this.ss = ss;
	}

	public void run() {
		for (int i = 0; i < 20; i++) {
			WoTou wt = new WoTou(i);
			ss.push(wt);
			System.out.println(Thread.currentThread().getName() + "   :" + wt);
			try {
				Thread.sleep((int) (Math.random() * 200));
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

class Consumer implements Runnable {
	SyncStack ss = null;

	Consumer(SyncStack ss) {
		this.ss = ss;
	}

	public void run() {
		for (int i = 0; i < 20; i++) {
			WoTou wt = ss.pop();
			System.out.println(Thread.currentThread().getName() + "   : " + wt);
			try {
				Thread.sleep((int) (Math.random() * 1000));
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}


좋은 웹페이지 즐겨찾기