java线程的典例

<pre name="code" class="java">/**
 * 创建一个简单的线程,通过继承Thread类
 * 
 * @author Al菜菜
 *
 */
public class ThreadDemo01 extends Thread {
	private String name;

	public ThreadDemo01(String name) {
		super();
		this.name = name;
	}

	@Override
	public void run() {
		for (int i = 0; i < 1000; i++) {
			System.out.println(name + ":" + i);
		}
	}

	public static void main(String[] args) {
		ThreadDemo01 td = new ThreadDemo01("me");
		ThreadDemo01 td1 = new ThreadDemo01("you");
		td.start();
		td1.start();
	}

}
</pre><pre name="code" class="java">
</pre><pre name="code" class="java">/**
 * 通过实现Runnable接口,创建一个线程
 * 
 * @author Al菜菜
 *
 */
public class ThreadDemo02 implements Runnable {
	private String name;

	public ThreadDemo02(String name) {
		this.name = name;
	}

	public static void main(String[] args) {
		ThreadDemo02 td = new ThreadDemo02("me");
		ThreadDemo02 td1 = new ThreadDemo02("you");
		Thread th=new Thread(td);
		Thread th1=new Thread(td1);
		th.start();
		th1.start();
	}

	@Override
	public void run() {
		for (int i = 0; i < 1000; i++) {
			System.out.println(this.name + ":" + i);
		}
	}

}


/**
 * 创建线程,通过实现Runnable接口进而实现资源的共享
 * 
 * @author Al菜菜
 *
 */
public class ThreadDemo03 implements Runnable {
	private int ticket = 10;

	public static void main(String[] args) {
		ThreadDemo03 td1 = new ThreadDemo03();
		Thread t1 = new Thread(td1, "线程一");
		Thread t2 = new Thread(td1, "线程二");
		t1.start();
		t2.start();
	}

	@Override
	public void run() {
		for (int i = 1; i <= 10; i++) {
			if (ticket > 0)
				System.out.println(Thread.currentThread().getName() + ticket--);
		}
	}

}
/**
 * 判断线程是否启动
 * 
 * @author Al菜菜
 *
 */
public class ThreadDemo04 implements Runnable {
	public static void main(String[] args) {
		ThreadDemo04 td4 = new ThreadDemo04();
		Thread t1 = new Thread(td4, "线程啊");
		System.out.println(t1.isAlive() ? "线程已经启动" : "线程还未启动");
		t1.start();
		System.out.println(t1.isAlive() ? "线程已经启动" : "线程还未启动");
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for (int i = 0; i < 10; i++) {
			System.out.println(i);
		}
	}

}
/**
 * 线程的强制执行
 * 
 * @author Al菜菜
 *
 */
public class ThreadDemo05 {
	public static void main(String[] args) {
		Thread t1 = new Thread(new Runnable() {
			@Override
			public void run() {
				for (int i = 0; i < 1000; i++) {
					System.out.println(Thread.currentThread().getName() + ":"
							+ i);
				}
			}
		});
		t1.start();
		for (int i = 0; i < 1000; i++) {
			if (i > 10) {
				try {
					t1.join();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			System.out.println(Thread.currentThread().getName() + ":" + i);
		}
	}

}


/**
 * 线程的休眠
 * @author Al菜菜
 *
 */
public class ThreadDemo6 implements Runnable{
	public static void main(String[] args) {
		ThreadDemo6 t=new ThreadDemo6();
		new Thread(t).start();;
	}

	@Override
	public void run() {
		for(int i=0;i<100;i++){
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName()+":"+i);
		}
	}

}

/**
 * 线程的打断
 * 
 * @author Al菜菜
 *
 */
public class ThreadDemo07 {

	public static void main(String[] args) {
		Thread t1=new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(10000);
				} catch (InterruptedException e) {
					System.out.println("打断");
				}
			}
		});
		t1.start();
		try {
			Thread.sleep(2000);
			t1.interrupt();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}
/**
 * 后台线程
 * @author Al菜菜
 *
 */
public class ThreadDemo08 {

	public static void main(String[] args) {
		Thread t1=new Thread(new Runnable() {
			
			@Override
			public void run() {
				while(true){
					System.out.println("后台线程");
				}
			}
		});
		t1.setDaemon(true);
		t1.start();
		
		
	}

}
/**
 * 线程的优先级
 * @author Al菜菜
 *
 */
public class ThreadDemo09 implements Runnable{
	
	public static void main(String[] args) {
		ThreadDemo09 td9=new ThreadDemo09();
		Thread t =new Thread(td9);
		Thread t1 =new Thread(td9);
		Thread t2 =new Thread(td9);
		t.setPriority(5);
		t1.setPriority(8);
		t2.setPriority(10);
		t.start();
		t1.start();
		t2.start();
	}
	@Override
	public void run() {
		for(int i=0;i<100;i++){
			System.out.println(Thread.currentThread().getName()+":"+i);
		}
	}

}


/**
 * 线程的礼让
 * @author Al菜菜
 *
 */
public class ThreadDemo10 implements Runnable{

	public static void main(String[] args) {
		ThreadDemo10 td10=new ThreadDemo10();
		Thread th=new Thread(td10);
		Thread th1=new Thread(td10);
		th.start();
		th1.start();
	}
	@Override
	public void run() {
		for(int i=0;i<100;i++){
			if(i%3==0){
				System.out.println("线程礼让");
				Thread.yield();
			}
			System.out.println(Thread.currentThread().getName()+":"+i);
		}
	}

}

/**
 * 买票问题
 * @author Al菜菜
 *
 */
public class ThreadDemo11 implements Runnable{
	private int a=10;
	public static void main(String[] args) {
		ThreadDemo11 t11 = new ThreadDemo11();
		Thread t1=new Thread(t11);
		Thread t2=new Thread(t11);
		Thread t3=new Thread(t11);
		t1.start();
		t2.start();
		t3.start();
	}

	@Override
	public void run() {
		for(int i=0;i<50;i++){
			synchronized (this) {
				if(a>0){
					try {
						Thread.sleep(200);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println("a:"+a--);
				}
			}
		
		}
	}

}
/**
 * 消费者和生产者的问题(比较好的实现方法,把要生产和消费的产品利用封装的特性包装在一个类中)
 * 
 * @author Al菜菜
 *
 */
public class ThreadDemo12 {
	public static void main(String[] args) {
		goods g=new goods();
		Product pro=new Product(g);
		Thread th1=new Thread(pro);
		th1.start();
		Customer cus=new Customer(g);
		Thread th2=new Thread(cus);
		th2.start();
	}

}
/**
 * 商品类
 * @author Al菜菜
 *
 */
class goods {
	private String name;
	private int num;

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public int getNum() {
		return num;
	}
	/**
	 * 消费后将商品设为空值
	 */
	public void setEmpty(){
		this.name=null;
		this.num=0;
	}
	/**
	 * 判断是否有商品可以消费
	 * @return
	 */
	public boolean isEmpty(){
		return this.name==null&&this.num==0;
	}
}

class Product implements Runnable {
	private boolean flag = false;//用于循环生产商品
	private goods g;

	public Product(goods g) {
		this.g = g;
	}

	@Override
	public void run() {
		for (int i = 0; i < 30; i++) {
			synchronized (Object.class) { //线程锁
				if(!g.isEmpty()){
					try {
						Object.class.wait();  //当有商品没有被消费是,进入等待状态
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				if (flag) {
					System.out.print("生产了商品一");
					g.setName("商品一");
					try {
						Thread.sleep(100); //用于在不使用锁的情况下,触发现象
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println("生产了20个");
					g.setNum(20);
					flag=false;
				} else {
					System.out.print("生产了商品二");
					g.setName("商品二");
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println("生产了40个");
					g.setNum(40);
					flag=true;
				}
				Object.class.notify();
			}
		}
	}
}

class Customer implements Runnable {
	private goods g;

	public Customer(goods g) {
		this.g = g;
	}

	@Override
	public void run() {
		for(int i=0;i<30;i++){
			synchronized (Object.class) {
				if(g.isEmpty()){
					try {
						Object.class.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				System.out.println("消费商品:"+g.getName()+"数量:"+g.getNum());
				g.setEmpty();
				Object.class.notify();
			}
		}
	}
}
/**
 * 用java线程实现死锁
 * @author Al菜菜
 *
 */
public class ThreadDemo13 implements Runnable{

	public static void main(String[] args) {
		ThreadDemo13 td13 = new ThreadDemo13();
		Thread t1=new Thread(td13);
		Thread t2=new Thread(td13);
		t1.start();
		t2.start();
	}

	@Override
	public void run() {
		synchronized (Object.class) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			synchronized (this) {
				for(int i=0;i<100;i++){
					System.out.println(Thread.currentThread().getName()+":"+i);
				}
			}
			
		}
		synchronized (this) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			synchronized (Object.class) {
				for(int i=0;i<100;i++){
					System.out.println(Thread.currentThread().getName()+":"+i);
				}
			}
			
		}
	}

}






                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值