创建线程的两种方式和线程的安全性问题

一:创建线程的两种方式

1.继承Thread类

//继承Thread类实现多线程
//经典案例:火车票购票
class Train extends Thread{
	private int tic;

	@Override
	public void run() {
		for(tic=20;tic>0;tic--){
			System.out.println(tic);
		}
	}
	
	
}
public class ThreadDemo {

	public static void main(String [] args){
		new Train().start();
		new Train().start();
		new Train().start();
	}
}


2.实现Runnable接口

//实现Runnable类实现多线程
//经典案例:火车票购票
class Train implements Runnable{
	private int tic;

	@Override
	public void run() {
		for(tic=20;tic>0;tic--){
			System.out.println(tic);
		}
	}
	
	
}
public class ThreadDemo {

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

两种方式创建线程的优缺点:用于java中只有单继承,所以当一个类需要继承另一个类时,就只能用实现Runnable 接口的方式。

二:线程的安全性问题

//实现Runnable类实现多线程
//经典案例:火车票购票
class Train implements Runnable{
	private int tic;

	@Override
	public void run() {
		for(tic=20;tic>0;tic--){
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName()+"购买了第"+tic+"张票");
		}
	}
	
	
}
public class ThreadDemo {

	public static void main(String [] args){
		Train t1=new Train();
		new Thread(t1,"小明").start();
		new Thread(t1,"小王").start();
		new Thread(t1,"小张").start();
		new Thread(t1,"小李").start();
	}
}
从运行结果看出,出现了有人购买第0张票,甚至是负数。
解决方案:
1.将可能出现问题的代码放到Synchronized代码块,Synchronized方法中
//实现Runnable类实现多线程
//经典案例:火车票购票
class Train implements Runnable{
	private int tic=200;

	@Override
	public void run() {
		for(int i=0;i<200;i++){
			synchronized(this){
				if(tic>0){
					System.out.println(Thread.currentThread().getName()+"购买了第"+tic+"张票");
					tic--;
				}
			}
			
			
		}
	}
	
	
}
public class ThreadDemo {

	public static void main(String [] args){
		Train t1=new Train();
		new Thread(t1,"小明").start();
		new Thread(t1,"小王").start();
		new Thread(t1,"小张").start();
		new Thread(t1,"小李").start();
	}
}
//实现Runnable类实现多线程
//经典案例:火车票购票
class Train implements Runnable{
	private int tic=200;

	@Override
	public void run() {
		for(int i=0;i<200;i++){
			buy();
		}
	}
	
	synchronized private void buy(){
		if(tic>0){
			System.out.println(Thread.currentThread().getName()+"购买了第"+tic+"张票");
			tic--;
		}
	}
	
}
public class ThreadDemo {

	public static void main(String [] args){
		Train t1=new Train();
		new Thread(t1,"小明").start();
		new Thread(t1,"小王").start();
		new Thread(t1,"小张").start();
		new Thread(t1,"小李").start();
	}
}






2.Lock类
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

//实现Runnable类实现多线程
//经典案例:火车票购票
class Train implements Runnable{
	private int tic=200;
	Lock lock=new ReentrantLock();

	@Override
	public void run() {
		
		for(int i=0;i<200;i++){
			lock.lock();
				if(tic>0){
					System.out.println(Thread.currentThread().getName()+"购买了第"+tic+"张票");
					tic--;
				}
				lock.unlock();
			}
			
		
	}
	
	
}
public class ThreadDemo {

	public static void main(String [] args){
		Train t1=new Train();
		new Thread(t1,"小明").start();
		new Thread(t1,"小王").start();
		new Thread(t1,"小张").start();
		new Thread(t1,"小李").start();
	}
}




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值