Thread

Thread

  • 线程

    java.lang.Thread

    public class Thread	
    extends Object	
    implements Runnable
    

基本概念

  • 程序

一段指令,完成一个特定的功能或一组特定的功能。

  • 进程

一个程序被双击执行后,变成对应的进程

  • 线程

是线程的一个执行单元,进程应用线程做事,一个进程有多个线程,如果一个进程只有一个线程称之为单进程/单线程程序。

  • java采用的是多线程开发,python采用多进程开发。
  • 调用start方法启动线程

创建线程

1:继承

​ 1:继承Thread
​ 2:重写run方法 run方法中编写要执行的逻辑代码
​ 3:创建子类对象
​ 4:调用start方法 启动该线程

public static void main(String[] args) {//main主线程
		
		MyT m = new MyT();
		m.start();
		
		for(int i=0;i<5;i++) {
			System.out.println("main"+i);
		}
		
		
	}

}
class MyT extends Thread{
	@Override
	public void run() {
		for(int i=0;i<5;i++) {
			System.out.println("myt"+i);
		}
	}
}

2:实现

​ 1:实现Runnable
​ 2:重写run方法 run方法中编写要执行的逻辑代码
​ 3:创建Thread对象,将实现类对象作为参数传入Thread的构造器
​ 4:调用start方法 启动该线程

public static void main(String[] args) {
		
		MyRun m = new MyRun();
		Thread t = new Thread(m);
		t.start();
		
		for(int i=0;i<5;i++) {
			System.out.println("main"+i);
		}
		
	}

}
class MyRun implements Runnable{

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

3:实现

​ 1:Callable接口
​ 2:线程池

4:创建匿名内部类线程

	Thread t1 = new Thread(new Runnable() {

			@Override
			public void run() {
				for(int i=0;i<5;i++) {
					try {
						Thread.sleep(1);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					System.out.println(Thread.currentThread().getName()+"下开始发车"+i+"号车出发");
				}
				
			}
			
			
		},"紧急情况");
		t1.start();

让线程变阻塞

  • 方法

    Thread.sleep();

    t1.join 此时t1做完t2才执行;

    Thread.yield();线程礼让

    public static void main(String[] args) {
    		Thread t = new Thread(new Runnable() {
    
    			@Override
    			public void run() {
    				long start = System.currentTimeMillis();
    				for(int i=0;i<10000000;i++) {
    					Thread.yield();
    				}
    				long end = System.currentTimeMillis();
    				System.out.println(end-start);
    				
    			}
    			
    		});
    		t.start();
    	}
    

线程的同步

同步器,锁机制

synchronized


  • 修饰非静态方法:

    此时同一时间只有一个线程能访问该方法,该线程执行的方法完后才会将锁释放,其他的线程才能继续执行;

    对于非静态方法synchronized锁的对象其实是this对象

  • 修饰静态方法时

    synchronized锁的其实是当前类的class对象

  • 同步代码块

    synchronized(锁芯){

    }

    锁芯:对象

    非静态方法使用 首选this对象

    静态方法使用 首选类的class对象

  public class Test02 {
  	
  	public static void main(String[] args) {
  		TicketRun tr = new TicketRun();
  		for(int i=65;i<70;i++) {
  			new Thread(tr,"窗口"+(char)i).start();
  		}
  	}
  
  }
  class TicketRun implements Runnable{
  	int count = 10;
  
  	@Override
  	public void run() {
  		for(int i=0;i<10;i++) {
  			checkTicket();
  		}
  		
  	}
  
  	public synchronized void checkTicket() {
  		if(count>0) {
  			try {
  				Thread.sleep(1000);
  			} catch (InterruptedException e) {
  				// TODO Auto-generated catch block
  				e.printStackTrace();
  			}
  			System.out.println(Thread.currentThread().getName()+"正在售票:"+(count--));
  		}
  		
  	}
  	
  }

标准的单例模式

	public static void main(String[] args) {
		for(int i=0;i<5;i++) {
			new Thread(new Runnable() {

				@Override
				public void run() {
					System.out.println(Lazy.getInstance());
					
				}
				
			}).start();
		}
	}

}
class Lazy{
	private volatile static Lazy lazy = null;
	private Lazy() {
		if(lazy!=null) {
			throw new RuntimeException("别反射");
		}
	}
	
	public static Lazy getInstance() {
		if(lazy==null) {
			synchronized (Lazy.class) {
				if(lazy==null) {
					try {
						Thread.sleep(20);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					lazy = new Lazy();
				}
			}
		}
		
		return lazy;
	}
		
	
	public Object readResolve(){
		
		
		return lazy;
	
	
	
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值