java 通过实现Runnable接口实现多线程

package thread;

public class TestCooking {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Tang tang = new Tang();
		Thread tangTh = new Thread(tang);
		Cooking cooking = new Cooking();
		Thread cookingTh = new Thread(cooking);
		tangTh.start();
		cookingTh.start();
	}

}

class Tang implements Runnable{
	public void bao(){
		for (int i = 1; i <= 4; i++) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("枸杞老鸭汤,煲了"+i+"个小时了");
			
		}
		System.out.println("汤好了");
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		bao();
	}
}

class Cooking implements Runnable{
	public void cook(){
		for (int i = 1; i <= 4; i++) {
			try {
				Thread.sleep(600);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("正在炒个"+i+"菜");
		}
		System.out.println("菜好了");
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		cook();
	}
}

你可能感兴趣的:(Java基础知识)