多线程基础

本文详细介绍了Java中实现多线程的两种方法(继承Thread和实现Runnable),展示了如何使用synchronized关键字管理线程同步,以及常见线程操作如启动、中断、等待、唤醒等。实例演示了并发编程中可能遇到的死锁问题,重点讲解了Synchronized锁的作用和避免死锁的策略。
摘要由CSDN通过智能技术生成
  1. 实现多线程的方法:
package main.java.multipleThread;
/**
 * 方法1:继承Thread类,并重写run方法 
 */
public class MultipleThread1 extends Thread {
    @Override    
    public void run() {
		System.out.print("multipleThread1启动");
    }
}

/**
 1. 方法2:实现Runnable接口,并重写run方法 
 */
public class MultipleThread2 implements Runnable {
    @Override
    public void run() {
		System.out.print("multipleThread2启动");    
    }
}
  1. 常用方法总结:
package main.java.multipleThread;
/**
 * 常用方法总结
 */
public class MultipleThread4 extends Thread {
    MultipleThread1 thread1 = new MultipleThread1();    
    public void main(String[] args) throws InterruptedException {
		/**        
		 * 设置当前线程名         
		 */        
		thread1.setName("threadNew");      
 
		/**        
 		 * 获取当前线程名         
 		 */        
		thread1.getName();        

		/**        
 		 * 启动线程,并执行run方法         
 		 */        
		thread1.start();        

		/**        
 		 * 调用run方法并不会启动线程         
		 */        
		thread1.run();        

		/**        
		 * 返回当前线程         
		 */        
		Thread.currentThread();        

		/**        
		 * 获取线程优先级         
		 */        
		thread1.getPriority();        

		/**        
  		 * 设置线程优先级         
		 */        
		thread1.setPriority(1);        

		/**        
		 * 中断线程,运行态 ——》 死亡态         
		 */        
		thread1.interrupt();        

		/**        
		 * 判断线程是否处于活动状态,即开启start方法后,线程就为活动状态         
		 */        
		thread1.isAlive();       

		/**        
		 * 底层封装的是object的wait方法,所以会释放锁,线程进入阻塞态。         
		 * 比如t1线程是下单,t2线程是减库存,t1启动,执行到一半的时候在t1里启动t2,        
		 * t2调用join(1000L)的方法,代表t1线程进入阻塞态,t2继续执行,如果到1000毫秒仍然没有执行完,t1则继续执行         
		 */        
		thread1.join(1000L);        

		/**        
		 * 当前时间线程睡眠指定时间,让出cpu使用权,不会释放锁,一旦睡眠时间结束		就会立即运行         
		 * 应该是运行态 ——》 就绪态         
		 */        
		thread1.sleep(1000L);        

		/**        
		 * 当前线程进入就绪态,让出处理机,不会释放锁         
		 */        
		thread1.yield();        

		/**         
		 * 线程进入阻塞态,并释放锁,释放的是this的锁,会被其他线程执行notify的时候唤醒         
		 * object类的方法,不是线程的方法         
		 */        
		thread1.wait();        

		/**        
		 * 唤醒被wait的其他线程,本线程让出处理机???进入就绪态???         
		 * object类的方法,不是线程的方法         
		 */        
		thread1.notify();        

		/**        
		 * 唤醒所有被wait的线程         
		 * object类的方法,不是线程的方法         
		 */        
		thread1.notifyAll();    
	}
}
  1. 关于Synchronized锁
package main.java.multipleThread;
/**
 5. synchronized锁 
 */
public class SynchronizedIntro {
    Object object = new Object();    
    static Object object1 = new Object();    
    int t = 0;    
    static int s = 0;    
    /**    
     * 对象锁,锁的是this,也就是当前对象,在调用此方法时会被synchronized影响     * 以下两种写法等同     
     */    
    public synchronized void doSome1() {
		t ++;    
    }    
    public void dosome11() {
		synchronized (this) {
			t ++;        
        }    
    }    
    /**    
     * 对象锁,锁的是object对象     
     */    
    public void doSome2() {
 		synchronized (object) {
			t = t * 2;        
        }    
    }    
    /**    
     * 类锁,锁的是当前类或指定类的Clas对象     
     */    
    public static synchronized void doSome3() {
		s = s * 2;    
    }    
    /**    
     * 类锁,锁的object1对应object1的静态变量,整个jvm也只有一个object1,所以相当于类锁     
     */    
    public static void doSome4() {
		synchronized (object1) {
			s ++;        
        }    
    }
 }
  1. 阿里相关笔试题:
    以下程序可能出现什么问题?
    wait() 相当于 this.wait()
    notify() 相当于 this.notify
    所以如果pop所以如果pop@TOC抢占到了虚拟机,进入了两层synchronized,此时list的size = 0,wait会释放锁,但是只释放thread3对象上的锁,即pop方法修饰的synchronized,而对list的锁并没有释放,会导致死锁
public class MultipleThread3 {
    LinkedList list = new LinkedList();

    public synchronized void push() {
        synchronized (list) {
            list.addLast(new Object());
            notify();
        }
    }
    
    public synchronized void pop() throws InterruptedException {
        synchronized (list) {
            if (list.size() <= 0) {
                wait();
            }
            list.pollLast();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MultipleThread3 thread3 = new MultipleThread3();
        thread3.push();
        thread3.pop();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值