线程创建
* 创建线程的方式有两种 , 一种是实现Runnable接口 . 另一种是继承Thread类
* 继承类的可以直接创建线程对象 , 实现接口的不行 , 得创建实现类的对象
实现Runnable接口
class Processor_02 implements Runnable {
@Override
public void run ( ) {
for ( int i = 0 ; i < 5 ; i++ ) {
System. out. println ( Thread. currentThread ( ) . getName ( ) + i) ;
}
}
}
继承Thread类
class Processer_01 extends Thread {
@Override
public void run ( ) {
for ( int i = 0 ; i < 5 ; i++ ) {
System. out. println ( "线程测试1 :" + i) ;
}
}
}
线程的生命周期 sleep currentThread
* 线程的生命周期 : 创建 , 就绪 , 运行 , 阻塞 ( 就绪, 运行, 阻塞. . . . . ) , 死亡
*
* currentThread ( ) : 静态方法, 用于获取当前线程的对象, 写在哪个线程中, 就获取哪个线程的对象
*
* sleep ( long m) : 静态方法, 让当前线程睡眠, 需要传入对应的毫秒数, 写在哪个线程就睡眠哪个线程
*
* 这两个方法都是静态方法 , 是在哪里调用的就是谁 , 与是哪个对象调用的无关 , 至于方法有关
* 比如 , 在main方法里调用 , 但是有时候很容易出错 , 比如
* 在调用的时候在他前面加个对象再调用 , 依然是main调用的 , 而不是哪个对象 , 因为
* 在编译阶段 , 编译器会直接将这个错误语法纠正成为方法名 , 然后再编译
public class Thread_Lifecycle {
public static void main ( String[ ] args) {
Thread thread2 = new Thread ( new Processor_02 ( ) ) ;
thread2. start ( ) ;
for ( int i = 0 ; i < 5 ; i++ ) {
try {
Thread. sleep ( 1000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
System. out. println ( Thread. currentThread ( ) . getName ( ) ) ;
}
}
}
interrupt() 强制唤醒
* interrupt ( )
* 强制唤醒 : 相当于一个是睡到自然醒 , 一个是被闹铃叫醒 , 被闹铃叫醒就是强制唤醒
public class Thread_Interrupt {
public static void main ( String[ ] args) {
Thread thread3 = new Processer_03 ( ) ;
thread3. start ( ) ;
try {
Thread. sleep ( 3000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
thread3. interrupt ( ) ;
}
}
class Processer_03 extends Thread {
@Override
public void run ( ) {
try {
Thread. sleep ( 300000 ) ;
System. out. println ( "哎呀,睡醒了" ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
System. out. println ( "叮叮叮~闹钟响了" ) ;
}
System. out. println ( "开始上班" ) ;
}
}
优先级
* 线程有十个等级的优先级
* 分别是1 ~ 10
* 在Thread类中 , 定义了三个常量 , 代表了三个优先级别
* 最高 10 Thread. MAX_PRIORITY
* 正常 5 Thread. NORM_PRIORITY
* 最高 1 Thread. MIN_PRIORITY
* 获取该线程的优先级 : getPriority ( ) ;
* 设置该线程的优先级 : setPriority ( ) ;
*
* 线程创建时会继承父线程的优先级 , Thread类的优先级是5
* 优先级只是代表在同时抢占一个资源的时候会优先响应优先级别高的 , 所以并不是优先级越高
* 就越先执行
public class Thread_Priority {
public static void main ( String[ ] args) {
Thread thread1 = new Processer_01 ( ) ;
Thread thread2 = new Thread ( new Processor_02 ( ) ) ;
System. out. println ( thread1. getPriority ( ) ) ;
System. out. println ( thread2. getPriority ( ) ) ;
thread1. setPriority ( Thread. MIN_PRIORITY ) ;
thread2. setPriority ( Thread. MAX_PRIORITY ) ;
System. out. println ( thread1. getPriority ( ) ) ;
System. out. println ( thread2. getPriority ( ) ) ;
thread1. start ( ) ;
thread2. start ( ) ;
}
}
Thread.yield() 让时间片
* Thread. yield ( ) ; 静态方法 , 暂停执行当前方法 , 且开始执行其他方法
* 相同优先级就会让行 , 如果优先级较低 , 就不会让
public class Thread_Yield {
public static void main ( String[ ] args) {
Thread t1 = new Processor_05 ( ) ;
t1. start ( ) ;
for ( int i = 0 ; i < 5 ; i++ ) {
System. out. println ( "main = " + i) ;
}
}
}
class Processor_05 extends Thread {
@Override
public void run ( ) {
for ( int i = 0 ; i < 5 ; i++ ) {
Thread. yield ( ) ;
System. out. println ( getName ( ) + " = " + i) ;
}
}
}
stop 终止线程
* stop : 终止一个线程, 容易出现问题, 不太安全, 不推荐使用
* 建议优雅的终止一个线程 : 使用标识符解决( true 就结束线程 或者false 就结束都可以)
public class Thread_Stop {
public static void main ( String[ ] args) {
Thread t = new Processor_06 ( ) ;
t. start ( ) ;
try {
Thread. sleep ( 3000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
Processor_06 p = ( Processor_06) t;
p. isStop = true ;
}
}
class Processor_06 extends Thread {
boolean isStop = false ;
@Override
public void run ( ) {
for ( int i = 0 ; true ; i++ ) {
if ( isStop) {
return ;
}
System. out. println ( Thread. currentThread ( ) . getName ( ) + " = " + i) ;
try {
Thread. sleep ( 500 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
}
join 线程合并
public class Thread_Join {
public static void main ( String[ ] args) throws InterruptedException {
Thread thread = new Thread ( new Processer_04 ( ) ) ;
thread. start ( ) ;
thread. join ( ) ;
for ( int i = 0 ; i < 5 ; i++ ) {
System. out. println ( Thread. currentThread ( ) . getName ( ) + " = " + i) ;
try {
Thread. sleep ( 500 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
}
class Processer_04 implements Runnable {
@Override
public void run ( ) {
for ( int i = 0 ; i < 5 ; i++ ) {
System. out. println ( Thread. currentThread ( ) . getName ( ) + " = " + i) ;
try {
Thread. sleep ( 500 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
}
synchronized S锁
* 线程同步 : 当多个线程操作同一个数据的时候, 为了保证数据的一致
* 线程同步本质就是数据同步, 是一种安全机制
* 就是在某一线程操作一个数据的时候 , 其他线程需要访问这个数据就需要等待
* S锁 L锁 , 大体上与PV原语差不多 , 拿资源 , 在使用这个资源的时候 ,
* 在V原语没有释放这个资源之前, 其他人不许用
*
*
* 异步编程 : 线程之间是完全独立的, 谁的运行也不会受到别的线程的影响
*
* 只要对方法加上 synchronized 的成员方法, 就代表该方法不能被多个线程同时访问
public class Thread_synchronized {
public static void main ( String[ ] args) {
Account account = new Account ( 500 ) ;
Thread t1 = new Thread ( new Processor_07 ( account) ) ;
Thread t2 = new Thread ( new Processor_07 ( account) ) ;
t1. start ( ) ;
t2. start ( ) ;
}
}
class Processor_07 implements Runnable {
private Account account;
public Processor_07 ( Account account) {
this . account = account;
}
@Override
public void run ( ) {
account. withDraw ( 100 ) ;
System. out. println ( Thread. currentThread ( ) . getName ( )
+ " 取款成功,取款1000元,余额是 : " + account. getBalance ( ) ) ;
}
}
class Account {
private double balance;
public Account ( double balance) {
this . balance = balance;
}
public double getBalance ( ) {
return balance;
}
public void setBalance ( double balance) {
this . balance = balance;
}
public synchronized void withDraw ( double money) {
double after = balance - money;
try {
Thread. sleep ( 500 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
balance = after;
}
}
Lock L锁
* Lock锁 , 只能修饰语句块 Lock只有代码块锁,synchronized 有代码块锁和方法锁
* 使用Lock锁,JVM将花费较少的时间来调度线程,性能更好。并且具有更好的扩展性(提供更多的子类)
*
* 只是开启加锁和关闭锁 都是需要手动 , 所以他又叫显式锁 , synchronized 是隐式锁, 超出作用域 自动解锁
*
public class Thread_Lock {
ATM atm = new ATM ( 5000 ) ;
Thread t1 = new Thread ( new Processor_08 ( atm) ) ;
Thread t2 = new Thread ( new Processor_08 ( atm) ) ;
}
class Processor_08 implements Runnable {
private ATM atm;
public Processor_08 ( ATM atm) {
this . atm = atm;
}
@Override
public void run ( ) {
atm. withDraw ( 1000 ) ;
}
}
class ATM {
private double balance;
private Lock lock = new ReentrantLock ( ) ;
public ATM ( double balance) {
super ( ) ;
this . balance = balance;
}
public void withDraw ( double money) {
System. out. println ( Thread. currentThread ( ) . getName ( ) + " 进来了" ) ;
lock. lock ( ) ;
try {
double after = balance - money;
balance = after;
System. out. println ( Thread. currentThread ( ) . getName ( ) + "取钱成功,取钱"
+ money + ",剩余" + balance + "元" ) ;
} finally {
lock. unlock ( ) ;
}
}
}