一.线程与进程
1.1 什么是进程,什么是线程?
进程是一个应用程序
线程是引用程序中的一个执行场景/执行单元
1.2 进程和线程之间的关系
进程A和进程B的内存独立不共享。
比如QQ和微信的内存不共享,是两个引用程序
在java中线程A和线程B的方法区,堆内存共享
栈不共享
比如:
火车站,可以看做是一个进程。
火车站中的每一个售票窗口可以看做是一个线程。
我在窗口1购票,你可以在窗口2购票,你不需要等我,我也不需要等你。
所以多线程并发可以提高效率。
java中之所以有多线程机制,目的就是为了提高程序的处理效率。
二.线程的创建和启动
java语言中,实现线程有三种方式:
方法一
/*怎么创建线程对象? new就行了
怎么启动线程? 调用线程对象的start方法
*/
public class ThreadTest01 {
public static void main(String[] args) {
//创建一个线程对象
MyThread myThread = new MyThread();
//如果是myThread.run();那么就只是调用了myThread的一个方法而已
//只是在主线程的栈中压栈,并不会创建一个线程
//myThread.run();
//那么如何创建一个线程呢?
//myThread.start();这个方法可以创建一个线程出来,就是会多一个分支栈
//这个方法执行速度极快,栈内存一开辟就结束了
//接着就会自动调用myThread.run()方法,但是在分支栈中压栈和弹栈了,run和main是同一级别的
myThread.start();
for (int i = 0; i < 1000; i++) {
System.out.println("main--> " + i);
}
}
}
//编写一个类,直接继承 java.lang.Thread,重写run方法
class MyThread extends Thread{
@Override
public void run() {
//打印1-1000的数据
for (int i = 0; i < 1000; i++) {
System.out.println("myThread--> " + i);
}
}
}
方法二
(一)
public class ThreadTest02 {
public static void main(String[] args) {
//如何创建一个线程?
//创建一个可运行的类
MyThread2 myThread2 =new MyThread2();
//将可运行的类封装成一个线程对象
Thread thread1 = new Thread(myThread2);
//或者直接
Thread thread2 = new Thread(myThread2);
//启动线程
thread2.start();
//main方法
for (int i = 0; i <1000 ; i++) {
System.out.println("main --> " + i);
}
}
}
//这并不是一个线程类,只是一个可运行的类,它还不是一个线程
class MyThread2 implements Runnable{
@Override
public void run() {
for (int i = 0; i <1000 ; i++) {
System.out.println("myThread --> " + i);
}
}
}
(二)匿名内部类实现
public class ThreadTest03 {
public static void main(String[] args) {
//匿名内部类
Thread thread =new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println("分支线程 ------>" + i);
}
}
});
//启动分支线程
thread.start();
//main方法
for (int i = 0; i < 1000; i++) {
System.out.println("主线程 ------>" +i);
}
}
}
方法三
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;//JUC包下的,属于java并发包,老JDK中没有这个包
/*
* 实现线程的第三种方式
* 实现Callable接口
*
* 优点:可以获取到线程的执行结果
* 缺点:效率较低,在获取t线程的执行结果的时候,当前线程受阻塞,效率较低。
* JDK8 的新特性
*
* */
public class ThreadTest14 {
public static void main(String[] args) {
FutureTask futureTask = new FutureTask(new Callable() {
@Override
public Object call() throws Exception {//call() 相当于 run() 方法 ,只不过这个有返回值
System.out.println("call() begin");
Thread.sleep(10 * 1000);
int a =100;
int b =200;
System.out.println(Thread.currentThread().getName() + " end");
return a+b;//自动装箱
}
});
//创建线程对象
Thread thread = new Thread(futureTask);
thread.setName("钢铁侠");
//启动
thread.start();
//怎么获取线程的返回值呢?
//这是main方法,这是主线程
//在主线程中怎么获取t线程的放回结果
//get()方法的执行会导致“当前线程的阻塞”
try {
Object object = futureTask.get();//获取返回值
System.out.println("返回值是 " + object);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
//main方法这里的程序想要执行必须等待get()方法的结束
//而get()方法可能要很久,因为get()方法是为了拿另一个线程的执行结果
//另一个线程的执行时需要时间的
System.out.println("Hello World");
}
}
三.线程的生命周期
线程的声明周期
四.线程中的常用方法
(一)
/*1.怎么获取当前线程对象?
Thread t = Thread.currentThread()
* 2.获取线程对象的名字
* String name = 线程对象.getName();
*
* 3.修改线程对象的名字
* 线程对象.setName("线程名");
*
* 4.当线程没有设置名字的时候,默认的名字有什么规律?
* Thread-0
* Thread-1
* Thread-2
* ....
* */
public class ThreadTest04 {
public static void main(String[] args) {
//因为 currentThread 出现在main线程中
Thread thread1= Thread.currentThread();
System.out.println(thread1);//Thread[main,5,main]
//当前线程是main线程
System.out.println(thread1.getName());//main
//创建一个新线程
MyThread3 myThread3 = new MyThread3();
System.out.println(myThread3);//Thread[Thread-0,5,main]
//启动新线程
//myThread3.start();//Thread-0
//修改线程的名字
myThread3.setName("Thread1");
//启动线程
myThread3.start();
//main线程
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + " ---> " + i);
}
}
}
class MyThread3 extends Thread{
public void run(){
//获取当前线程的名字
System.out.println(currentThread().getName());
for (int i = 0; i < 100; i++) {
System.out.println(currentThread().getName() + " ---> " + i);
}
}
}
(二)
/*
* 关于线程的sleep()方法
* 1.静态方法
* 2.参数是毫秒
* 3.作用:让当前线程进入休眠,进入“阻塞状态”,放弃占有CPU时间片,让给其它线程使用
* 这行代码出现在A线程中,A线程就会进入休眠。
* 这行代码出现在B线程中,B线程就会进入休眠。
* 4.Thread.sleep()方法,可以做到这样的效果:
* 间隔特定的时间,去执行一段特定的代码,每隔多久执行一次
* */
public class ThreadTest05 {
public static void main(String[] args) {
/*//在main进程中,所以使用sleep方法,是使得main方法进入阻塞状态
try {
Thread.sleep(1000 * 5);
} catch (InterruptedException e) {
e.printStackTrace();
}
//五秒后执行此代码
System.out.println("Hello World");
*/
//使得每个一秒钟输出一个数字
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(i);
}
}
}
/*
* Thread.sleep()方法的面试题
* */
public class ThreadTest06 {
public static void main(String[] args) {
//创建线程对象
MyThread5 myThread = new MyThread5();
myThread.setName("ttt");
myThread.start();
//请问这会使得 myThread 休眠一秒吗
try {
myThread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//不会,因为sleep是一个静态方法,最终在编译器会看做Thread.sleep() 这个的作用是休眠当前线程
//当前线程是main线程
System.out.println("Hello World");
//Hello World
//0
//1
//2
//3
//4
}
}
class MyThread5 extends Thread{
@Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(i);
}
}
}
(三)
/*
* 怎么唤醒正在休眠的线程?
* 不是终断线程的执行,是终止线程的睡眠
* */
public class ThreadTest07 {
public static void main(String[] args) {
//创建线程对象
Thread myThread6 =new Thread(new MyThread6());
myThread6.setName("钢铁侠 Thread");
myThread6.start();
//希望过5秒能让 myThread6 醒过来
//一年太长了
try {
//当前线程是主线程
Thread.sleep(1000 * 5);
} catch (InterruptedException e) {
e.printStackTrace();
}
//这个方法是利用了异常处理
终止t线程的睡眠
myThread6.interrupt();
}
}
class MyThread6 implements Runnable{
@Override
public void run() {
//休眠一年
System.out.println(Thread.currentThread().getName() + " ---> begin");
try {
Thread.sleep(365 * 24 * 60 * 60 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//一年之后再执行
System.out.println(Thread.currentThread().getName() + "---> over");
}
}
(四)
//在java中怎么强行终止一个线程的执行
public class ThreadTest08 {
public static void main(String[] args) {
MyThread7 myThread7 =new MyThread7();
myThread7.setName("美国队长 Thread");
myThread7.start();
//过五秒后让 myThread7 终止
try {
Thread.sleep(1000 * 5);
} catch (InterruptedException e) {
e.printStackTrace();
}
myThread7.stop();//已过时,不建议使用
}
//stop存在缺点,容易丢失数据,因为直接将线程杀死了
//美国队长 Thread --> 0
//美国队长 Thread --> 1
//美国队长 Thread --> 2
//美国队长 Thread --> 3
//美国队长 Thread --> 4
}
class MyThread7 extends Thread{
public void run(){
for (int i = 0; i < 100; i++) {
//每个一秒输出一个数字
System.out.println(Thread.currentThread().getName() + " --> " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
(五)
/*
* 怎么合理的终止一个线程的执行?
* */
public class ThreadTest09 {
public static void main(String[] args) {
MyThread8 myThread =new MyThread8();
Thread myThread8 = new Thread(myThread);
myThread8.setName("黑豹");
myThread8.start();
//模拟五秒
try {
Thread.sleep(1000 * 5);
} catch (InterruptedException e) {
e.printStackTrace();
}
//想什么时候关闭,什么时候讲run 变成false就行了
myThread.run = false;
}
}
class MyThread8 implements Runnable{
//布尔标记
boolean run = true;
@Override
public void run() {
for (int i = 0; i < 100; i++) {
if (run){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(i);
}else{
//有什么要保存的在这保存
return;
}
}
}
}
(六)
//了解:关于线程的优先级
public class ThreadTest010 {
public static void main(String[] args) {
//线程的优先级
System.out.println(Thread.MAX_PRIORITY);//10
System.out.println(Thread.NORM_PRIORITY);//5
System.out.println(Thread.MIN_PRIORITY);//1
//将main线程的优先级变为1
Thread.currentThread().setPriority(1);
Thread myThread9 = new Thread(new MyThread9());
myThread9.setName("蜘蛛侠");
优先级较高的,只是抢到的CPU时间片相对多一些
myThread9.start();
for (int i = 0; i < 1000; i++) {
System.out.println("main --->" + i);
}
}
}
class MyThread9 implements Runnable{
@Override
public void run() {
System.out.println("线程的默认优先级是 " + Thread.currentThread().getPriority());
for (int i = 0; i < 1000; i++) {
System.out.println(Thread.currentThread().getName() + "--->" + i);
}
}
}
(七)
/*
* 让位:
*当前线程暂停,回到就绪转态,让给其它线程
* static void yield() 静态方法*/
public class ThreadTest11 {
public static void main(String[] args) {
Thread myThread = new MyThread10();
myThread.setName("雷神");
myThread.start();
for (int i = 0; i < 1000; i++) {
System.out.println("main ---> " +i);
}
}
}
class MyThread10 extends Thread{
public void run(){
for (int i = 0; i < 1000; i++) {
//每输出100就让位一次
if (i %100 == 0){
Thread.yield();//当前线程暂停一下,让给主线程
}
System.out.println(Thread.currentThread().getName() + "---> " + i);
}
}
}
(八)
/*
* 线程的合并
* */
public class ThreadTest12 {
public static void main(String[] args) {
System.out.println("main begin!!!");
Thread myThread11 = new MyThread11();
myThread11.setName("黑寡妇");
myThread11.start();
try {
//不是栈的合并,是栈之间的协调
myThread11.join();//myThread11 合并到 主线程,主线程进入阻塞状态,当 myThread11 执行完再执行
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("main over");
}
//main begin!!!
//0
//1
//2
//3
//4
//main over
}
class MyThread11 extends Thread{
@Override
public void run() {
for (int i = 0; i <5 ; i++) {
System.out.println(i);
}
}
}
五.线程安全
(一)模拟银行操作系统
public class Test {
public static void main(String[] args) {
//创建银行账户
Account account =new Account("美国队长的账户",10000);
//创建两个线程
//猎鹰和美国队长同时去取钱
AccountThread accountThread1 = new AccountThread(account);
AccountThread accountThread2 = new AccountThread(account);
accountThread1.setName("美国队长");
accountThread2.setName("猎鹰");
//取款
accountThread1.start();
accountThread2.start();
//猎鹰 对 美国队长的账户 取款了5000.0元,余额是 5000.0
//美国队长 对 美国队长的账户 取款了5000.0元,余额是 5000.0
//这就出错了
}
}
public class AccountThread extends Thread{
//同一个进程有相同的账号对象
//模拟两个人同时去ATM机器取钱
//has a
private Account account;
//构造方法,传入银行账户属性
public AccountThread(Account account) {
this.account = account;
}
@Override
//run方法的执行表示取款操作
public void run() {
//假设就取5000元钱
double money = 5000;
account.withdraw(money);
//取款信息
System.out.println(Thread.currentThread().getName() + " 对 " + account.toString() + " 取款了" + money + "元,余额是 " + account.getBalance());
}
}
//银行账户类
public class Account {
//账号
private String acton;
//余额
private double balance;
//构造方法
public Account() {
}
public Account(String acton, double balance) {
this.acton = acton;
this.balance = balance;
}
//set和get方法
public String getActon() {
return acton;
}
public void setActon(String acton) {
this.acton = acton;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
//取款方法
public void withdraw(double money){
//取前余额
double before = this.balance;
//取后余额
double after = before - money;
//模拟网络延迟,就一定会出错
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//更新余额
setBalance(after);
}
@Override
public String toString() {
return acton;
}
}
(二)synchronized第一种使用方式
public class Test {
public static void main(String[] args) {
//创建银行账户
Account account =new Account("美国队长的账户",10000);
//创建两个线程
//猎鹰和美国队长同时去取钱
AccountThread accountThread1 = new AccountThread(account);
AccountThread accountThread2 = new AccountThread(account);
accountThread1.setName("美国队长");
accountThread2.setName("猎鹰");
//取款
accountThread1.start();
accountThread2.start();
//猎鹰 对 美国队长的账户 取款了5000.0元,余额是 5000.0
//美国队长 对 美国队长的账户 取款了5000.0元,余额是 5000.0
//这就出错了
}
}
public class AccountThread extends Thread{
//同一个进程有相同的账号对象
//模拟两个人同时去ATM机器取钱
//has a
private Account account;
//构造方法,传入银行账户属性
public AccountThread(Account account) {
this.account = account;
}
@Override
//run方法的执行表示取款操作
public void run() {
//假设就取5000元钱
double money = 5000;
account.withdraw(money);
//取款信息
System.out.println(Thread.currentThread().getName() + " 对 " + account.toString() + " 取款了" + money + "元,余额是 " + account.getBalance());
}
}
(三)synchronized第二种使用方式
//银行账户类
public class Account {
//账号
private String acton;
//余额
private double balance;
//构造方法
public Account() {
}
public Account(String acton, double balance) {
this.acton = acton;
this.balance = balance;
}
//set和get方法
public String getActon() {
return acton;
}
public void setActon(String acton) {
this.acton = acton;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
//取款方法
public void withdraw(double money){
//以下的代码必须是排队的,要不然就可能会出问题
//一个线程把这里的代码全部执行完之后,另一个线程才能进来
/*
* 线程同步代码的语法
* synchronized(){
}
* synchronized 是一个关键字
* 后面的小括号填的东西非常重要,填的是你想要同步的线程的数据
* 这个数据必须是多线程的共享数据,才能达到多线程的排队
*
* ()里面写什么?
* 那看你想让哪些线程同步?
* 假设t1,t2,t3,t4,t5,有五个线程
* 你只希望t1,t2,t3排队,t4,t5不需要排队,怎么办?
* 你就在()中写一个t1,t3,t3共享的对象,而这个对象对于t4 t5来说是不共享的
* */
/*
* 对于这里来说,银行的账户是共享的,就可以填银行的账户
* withdraw 是一个实例方法,所以this 就是银行对象
*
* 在java中,每一个对象都有一个对象锁
* 多少个对象,多少把锁
*
* 以下是线程同步的原理:
* t1和t2两个线程:
* 1. 假设t1和t2线程是并发的,那么肯定一先一后执行下面的代码
* 假设t1先执行到这个代码,t1看到 synchronized 关键字就会去找后面的“后面共享对象”的对象锁,
* 找到之后就会占有这把锁,然后执行同步代码块的代码,在程序中一直占有这把锁,直到t1执行完毕,才会释放这个
* “共享对象”的对象锁
* 2.假设t1已经占有了这把锁,t2这时候进到了synchronized中,去寻找对象锁,找到了对象锁,但是发现对象锁被t1占有了
* 那么也只能停下来,等到t1之星完毕,释放对象锁,才可以执行同步代码块
* 这样就达到了线程排队执行
* 这里要注意的是:这个共享对象一定要选好,
* 这个共享对象 是需要排队执行的这些线程对象所共享的
* */
//synchronized (object){ //也可以
//synchronized ("abc"){ //"abc"在字符串常量池当中,所有的线程都会等待
synchronized (this){
//取前余额
double before = this.balance;
//取后余额
double after = before - money;
//即使有网络延迟也没关系了
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//更新余额
setBalance(after);
}
}
@Override
public String toString() {
return acton;
}
}
package caopeng.javaseTest.threadsafetest.treadsafe3;
//银行账户类
public class Account {
//账号
private String acton;
//余额
private double balance;
//构造方法
public Account() {
}
public Account(String acton, double balance) {
this.acton = acton;
this.balance = balance;
}
//set和get方法
public String getActon() {
return acton;
}
public void setActon(String acton) {
this.acton = acton;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
//取款方法
//在方法的声明上使用synchronized,那么锁住一定是 this
//方法体就是同步代码块,可能会扩大同步的范围
//如果共享的就是this,并且需要同步的就是整个方法体,建议使用这个
//缺点:不够灵活
//优点:代码整洁
public synchronized void withdraw(double money){
//取前余额
double before = this.balance;
//取后余额
double after = before - money;
//模拟网络延迟,就一定会出错
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//更新余额
setBalance(after);
}
@Override
public String toString() {
return acton;
}
}
(四)synchronized第三种使用方式
在静态方法上用 synchronized 修饰,这个时候占用的是类锁,一个类一个锁,这个类的所有对象占有这一把锁
在下面的题目中最后一题有具体的代码实现
六.简单的Synchronized题目
(一)题目一
//问 doOther需要等 dosome 结束吗
public class Exam {
public static void main(String[] args) {
//创建一个myClass对象
MyClass myClass =new MyClass();
//创建两个线程对象
Thread thread1 = new MyThread(myClass);
Thread thread2 = new MyThread(myClass);
thread1.setName("钢铁侠");
thread2.setName("美国队长");
//启动线程
thread1.start();
try {
Thread.sleep(1000);//确保 钢铁侠 线程先执行
} catch (InterruptedException e) {
e.printStackTrace();
}
thread2.start();
}
//dosome begin!!!
//doOther begin!!!
//doOther over!!!
//dosome over!!!
//不需要,因为doOther没有synchronized修饰,不需要对象锁,即使 myclass 对象的对象锁的确被 钢铁侠线程占有了
}
class MyThread extends Thread{
MyClass myClass;
public MyThread(MyClass myClass){
this.myClass = myClass;
}
public void run(){
if(Thread.currentThread().getName().equals("钢铁侠")){
myClass.dosome();
}else if (Thread.currentThread().getName().equals("美国队长")){
myClass.doOther();
}
}
}
class MyClass{
public synchronized void dosome(){
System.out.println("dosome begin!!!");
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("dosome over!!!");
}
public void doOther(){
System.out.println("doOther begin!!!");
System.out.println("doOther over!!!");
}
}
(二)题目二
//问 doOther需要等 dosome 结束吗
public class Exam {
public static void main(String[] args) {
//创建一个myClass对象
MyClass myClass =new MyClass();
//创建两个线程对象
Thread thread1 = new MyThread(myClass);
Thread thread2 = new MyThread(myClass);
thread1.setName("钢铁侠");
thread2.setName("美国队长");
//启动线程
thread1.start();
try {
Thread.sleep(1000);//确保 钢铁侠 线程先执行
} catch (InterruptedException e) {
e.printStackTrace();
}
thread2.start();
}
//dosome begin!!!
//dosome over!!!
//doOther begin!!!
//doOther over!!!
//需要,因为 doOther此时也有Synchronized修饰,需要对象锁,但是doSome方法先执行,抢占了对象锁
//所以只能等 dosome方法执行完之后才能占有对象锁,然后执行同步代码块
}
class MyThread extends Thread{
MyClass myClass;
public MyThread(MyClass myClass){
this.myClass = myClass;
}
public void run(){
if(Thread.currentThread().getName().equals("钢铁侠")){
myClass.dosome();
}else if (Thread.currentThread().getName().equals("美国队长")){
myClass.doOther();
}
}
}
class MyClass{
public synchronized void dosome(){
System.out.println("dosome begin!!!");
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("dosome over!!!");
}
public synchronized void doOther(){
System.out.println("doOther begin!!!");
System.out.println("doOther over!!!");
}
}
(三)题目三
//问 doOther需要等 dosome 结束吗
public class Exam {
public static void main(String[] args) {
//创建两个myClass对象
MyClass myClass1 =new MyClass();
MyClass myClass2 =new MyClass();
//创建两个线程对象
Thread thread1 = new MyThread(myClass1);
Thread thread2 = new MyThread(myClass2);
thread1.setName("钢铁侠");
thread2.setName("美国队长");
//启动线程
thread1.start();
try {
Thread.sleep(1000);//确保 钢铁侠 线程先执行
} catch (InterruptedException e) {
e.printStackTrace();
}
thread2.start();
}
//dosome begin!!!
//doOther begin!!!
//doOther over!!!
//dosome over!!!
//不需要,因为现在有两个对象,对象锁,一个对象一把锁,多少个对象,多少把锁
//现在有两个对象, 钢铁侠 线程占有的是myClass1的对象锁
//与myClass2 的对象锁 没有关系,所以不会影响到 doOther方法的执行
}
class MyThread extends Thread{
MyClass myClass;
public MyThread(MyClass myClass){
this.myClass = myClass;
}
public void run(){
if(Thread.currentThread().getName().equals("钢铁侠")){
myClass.dosome();
}else if (Thread.currentThread().getName().equals("美国队长")){
myClass.doOther();
}
}
}
class MyClass{
public synchronized void dosome(){
System.out.println("dosome begin!!!");
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("dosome over!!!");
}
public synchronized void doOther(){
System.out.println("doOther begin!!!");
System.out.println("doOther over!!!");
}
}
(四)题目四
//问 doOther需要等 dosome 结束吗
public class Exam {
public static void main(String[] args) {
//创建两个myClass对象
MyClass myClass1 =new MyClass();
MyClass myClass2 =new MyClass();
//创建两个线程对象
Thread thread1 = new MyThread(myClass1);
Thread thread2 = new MyThread(myClass2);
thread1.setName("钢铁侠");
thread2.setName("美国队长");
//启动线程
thread1.start();
try {
Thread.sleep(1000);//确保 钢铁侠 线程先执行
} catch (InterruptedException e) {
e.printStackTrace();
}
thread2.start();
}
//dosome begin!!!
//dosome over!!!
//doOther begin!!!
//doOther over!!!
//需要,因为现在 Synchronized 修饰的是 静态方法,静态方法占有的是 类锁
//一个类一把锁,不管创建了几个对象,都是占用这一把锁, 钢铁侠 线程先执行,占用了MyClass的类锁
//美队执行到doOther的时候,发现没有这把锁,就要停下来等 钢铁侠 线程执行完毕
}
class MyThread extends Thread{
MyClass myClass;
public MyThread(MyClass myClass){
this.myClass = myClass;
}
public void run(){
if(Thread.currentThread().getName().equals("钢铁侠")){
myClass.dosome();
}else if (Thread.currentThread().getName().equals("美国队长")){
myClass.doOther();
}
}
}
class MyClass{
public synchronized static void dosome(){
System.out.println("dosome begin!!!");
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("dosome over!!!");
}
public synchronized static void doOther(){
System.out.println("doOther begin!!!");
System.out.println("doOther over!!!");
}
}
七.死锁
//死锁代码要会写,只有会写,才可能会调试
//死锁很难调试,执行不会结束,又不报异常
public class DeadLock {
public static void main(String[] args) {
Object object1 = new Object();
Object object2 = new Object();
//创建两个线程
Thread thread1 = new Thread1(object1,object2);
Thread thread2 = new Thread(new Thread2(object1,object2));
//启动线程
thread1.start();
thread2.start();
}
}
class Thread1 extends Thread{
Object object1;
Object object2;
public Thread1() {
}
public Thread1(Object object1, Object object2) {
this.object1 = object1;
this.object2 = object2;
}
public void run(){
synchronized (object1){
try {
Thread.sleep(1000 );
} catch (InterruptedException e) {
e.printStackTrace();
}
//嵌套 ,锁完 object1再锁object2
synchronized (object2){
}
}
}
}
class Thread2 implements Runnable{
Object object1;
Object object2;
public Thread2() {
}
public Thread2(Object object1, Object object2) {
this.object1 = object1;
this.object2 = object2;
}
@Override
public void run() {
synchronized (object2){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (object1){
}
}
}
}
八.守护线程
import org.w3c.dom.ls.LSOutput;
//守护线程
/*线程的分类:
* 1.用户线程,就是我们平常写的线程
* 2.守护线程,一般写成一个死循环,当所有的用户线程结束后,守护线程就结束了
* 注意:其中具有代表性的就是:垃圾回收线程(守护线程)。
* */
public class ThreadTest13 {
public static void main(String[] args) {
//创建线程对象
Thread myThread = new MyThread13();
myThread.setName("备份线程");
//在启动线程前,将线程改为守护线程
myThread.setDaemon(true);
//启动守护线程
myThread.start();
for (int i = 0; i < 15; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "----->" + i);
}
}
}
class MyThread13 extends Thread{
public void run(){
int i = 0;
while (true){
System.out.println(Thread.currentThread().getName() + "---->" + i);
i ++;
//每隔一秒备份一次
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
八.定时器
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
/*定时器的作用:
间隔特定的时间,执行特定的程序。
每周要进行银行账户的总账操作。
每天要进行数据的备份操作。
在实际的开发中,每隔多久执行一段特定的程序,这种需求是很常见的,
那么在java中其实可以采用多种方式实现:
可以使用sleep方法,睡眠,设置睡眠时间,没到这个时间点醒来,执行
任务。这种方式是最原始的定时器。(比较low)
在java的类库中已经写好了一个定时器:java.util.Timer,可以直接拿来用。
不过,这种方式在目前的开发中也很少用,因为现在有很多高级框架都是支持
定时任务的。
在实际的开发中,目前使用较多的是Spring框架中提供的SpringTask框架,
这个框架只要进行简单的配置,就可以完成定时器的任务。*/
public class TimerTest {
public static void main(String[] args) throws ParseException {
//创建定时器对象
Timer timer =new Timer();
//Timer timer =new Timer(true); 守护线程的方式
//指定定时任务
//timer.schedule(定时任务,第一次执行时间,间隔多久执行一次);
SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date firstTime = simpleDateFormat.parse("2020-04-22 15:46:00");
timer.schedule(new LogTimerTask(), firstTime,1000*10);
}
}
//java的类库中已经写好了一个定时器:java.util.Timer,可以直接拿来用。
//编写一个定时任务类
class LogTimerTask extends TimerTask{
@Override
public void run() {
SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
String strTime = simpleDateFormat.format(new Date());
System.out.println(strTime + "完成了一次数据备份");
}
}
九.wait()方法和notify()
import java.util.ArrayList;
import java.util.List;
/*
* 1.使用wait()方法和notify()方法实现“生产者和消费者模式”
* 2.什么是 “生产者和消费者模式” ?
* 生产线程负责生产,消费线程负责消费
* 生产线程和消费线程要达到均衡
* 这是一种特殊的业务需求,在这种特殊的情况下,需要使用 wait()方法和notify()方法
*
* 3.wait()方法和notify()方法不是线程对象的方法,是普通的java对象都有的方法
*
* 4.wait方法和notify()方法建立在线程同步的基础上,因为多线程要同时操作一个仓库,有线程安全问题
*
* 5.wait方法:o.wait()让正在o对象上活动的线程t进入等待状态,并且释放t线程之前占有的o对象的锁
* notify方法: 0.notify()让正在o对象上等待的线程唤醒,只是通知,不会释放o对象上之前占有的锁
*
* 6.模拟这样的需求:
* 仓库我们采取List集合
* List集合中假设只能存储一个元素
* 一个元素就表示仓库满了
* 如果List集合中元素个数是0,就表示仓库空了
* 保证:list集合中的元素永远最多存储一个元素
*
* 必须做到这样:生成一个消费一个
* */
public class ThreadTest15 {
public static void main(String[] args) {
//创建一个仓库对象
List list =new ArrayList();
//创建两个线程对象
//生产者线程
Thread thread1 = new Thread(new Producer(list));
//消费者线程
Thread thread2 = new Thread(new Consumer(list));
thread1.setName("生产者线程");
thread2.setName("消费者线程");
thread1.start();
thread2.start();
}
}
//生产者
class Producer implements Runnable{
private List list;
public Producer() {
}
public Producer(List list) {
this.list = list;
}
@Override
public void run() {
//一直生产
while(true){
synchronized (list){//占有list的对象锁
if (list.size() > 0){//说明仓库里面有东西
try {
list.wait();//让当前线程进入等待状态,放弃对象锁
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//程序执行到这,说明list集合是空的,可以生产
Object object =new Object();
list.add(object);
System.out.println(Thread.currentThread().getName() + "---->" + object);
//唤醒消费者,进行消费,唤醒不放弃锁
list.notify();
}
}
}
}
//消费线程
class Consumer implements Runnable{
private List list;
public Consumer() {
}
public Consumer(List list) {
this.list = list;
}
@Override
public void run() {
//一直消费
while(true){
synchronized (list){
if (list.size() == 0){//说明仓库里面没东西
try {
list.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}//程序执行到此,说明里面有数据,可以消费
Object object =list.remove(0);
System.out.println(Thread.currentThread().getName() + "---->" + object);
//唤醒生产者消费
list.notify();//唤醒不放弃锁
}
}
}
}