多线程
程序:一组命令的集合为了完成指定的功能程序是静态概念-般保存在硬盘当中
进程:正在运行的程序,是一个动态概念,需要保存在内存当中,操作系统会分配对应的PID,当我们直接关闭某个进程时候,该进行会在运行内存中被销毁
线程:一个程序中,不同的执行分支如果同一一个时间节点允许多个线程同时执行的时候,我们称为支持多线程
并行:多个cpu,同时执行多个任务
并发:一个cpu,同时执行多个任务(支持多线程) ..
多线程定义方法及方法
/**
*
* 第一种创建一个类,继承Thread类并覆写run方法 run方法就等于是新线程中的main方法
*
*/
public static void test01() {
// 创建线程类对象
Thread t1 = new A();
// 调用start方法启动线程
t1.start();
for (int i = 0; i < 10; i++) {
System.out.println(" main线程-->" + i);
}
}
/**
*
* 第二种创建一个类,实现Runnable接口,并覆写run方法 run方法就等于是新线程中的main方法
*
*/
public static void test02() {
// 创建线程类对象
B b = new B();
// 要使用thread类来实现
Thread t1 = new Thread(b);
// 调用start方法启动线程
t1.start();
for (int i = 0; i < 10; i++) {
System.out.println(" main线程-->" + i);
}
}
}
class A extends Thread {
@Override
public void run() {
int n = 1;
while (n < 10) {
System.out.print(n++);
}
}
}
class B implements Runnable {
@Override
public void run() {
int n = 1;
while (n < 10) {
System.out.print(n++);
}
}
}
public static void f() throws InterruptedException {
Thread c = new C();
// 开始
c.start();
// 得到名字
c.getName();
// 改名
c.setName("第一");
// 设置优先级
c.setPriority(1);
// 得到优先级
c.getPriority();
// 停止 c.stop(过时了)
// 静态方法。类调用继承Thread
// 获取当前线程对象
System.out.println(Thread.currentThread().getName());
Thread.currentThread().getName();
// 当前线程睡眠
Thread.sleep(2);
}
线程合并:
public class heBing {
public static void main(String[] args) throws InterruptedException {
Thread t = new D();
t.start();
// 在自己线程中调用别的线程的join 表示让他先执行完,自己再动
t.join();
int i = 1;
while (i < 10) {
System.out.print(i++);
System.out.print("main--");
}
}
}
class D extends Thread {
@Override
public void run() {
int n = 1;
while (n < 10) {
System.out.print(n++);
System.out.print("线程--");
}
}
}
yield :静态方法,暂停当前正在执行的线程对象,并执行其他等待中的线程
1静态方法,意味着跟那个对象调用没有关系,写在哪个线程中,哪个线程就让位
2给同优先级让位,不同优先级不让位
public class rangWei {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(new liZi());
t.setName("t1");
t.setPriority(4);
Thread.currentThread().setPriority(4);
t.start();
for (int i = 0; i < 10; i++) {
Thread.yield();
System.out.println(Thread.currentThread().getName() + "--->" + i);
Thread.currentThread().sleep(3000);
}
}
}
class liZi implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "--->" + i);
Thread.currentThread();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
简单控制:
public class kongZhi {
public static void main(String[] args) {
Processer_03 p = new Processer_03();
Thread t1 = new Thread(p);
t1.setName("t1");
t1.start();
try {
Thread.sleep(5000);
// t1.stop();
p.flag = true;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Processer_03 implements Runnable {
// 加一个标识,标识是否要终止线程
boolean flag = false;
@Override
public void run() {
for (int i = 0; true; i++) {
// 判断是否要终止
if (flag) {
System.out.println(Thread.currentThread().getName() + "线程已被终止");
return;
}
try {
Thread.sleep(1000);
System.out
.println(Thread.currentThread().getName() + "-->" + i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
线程同步:当多个线程有可能同时操作同一个数据的时候,为了保证数据一致性, 需要进行同步执行
数据安全机制
异步编程:线程之间是完全独立,相互没有影响
同步编程:线程之间不是完全独立,相互可能有影响: synchronized (语句块锁,方法锁),只能有一个线程执行
同步的场景: 1必须是多线程(必须有并发性,才有可能出错) 2多个线程有可能在同一-时间操作同-个数据的可能性
3尤其是同时对数据进行更改操作,查询无所谓
synchronized锁:
synchronized(对象){}成员语句块锁:锁对象
当访问一个对象中加锁的成员方法或者成员成员语句块锁的时候,则该对象中所有加锁的成员方法和成员语句块锁全部锁定
synchronized(类名.class)}静态语句块锁 :锁定类
当访问一个类中,加锁的静态方法或者静态语句块锁的时候,则该对象中所有加锁的静态方法和静态语句块锁全部锁定
public class tongBu {
public static void main(String[] args) {
// 创建账户,余额为3000
Account act = new Account(3000);
// 两个线程,每个取1000
Thread t1 = new xian(act);
Thread t2 = new xian(act);
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
}
// 线程类
class xian extends Thread {
Account a;
// 构造函数,传参
public xian(Account a) {
this.a = a;
}
// 线程执行函数
@Override
public void run() {
try {
// 当前对象调用函数
a.withDraw(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// 实体类
class Account {
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Account(double balance) {
super();
this.balance = balance;
}
// 现有的钱
private double balance;
// 取钱的方法
public void withDraw(double money) throws InterruptedException {
// 当前进程的对象的名字
System.out.println(Thread.currentThread().getName() + " 执行了");
Thread.sleep(5000);
// 语句块锁。锁当前对象this进程
synchronized (this) {
double after = balance - money;
balance = after;
System.out.println(Thread.currentThread().getName() + " 取钱成功,取款 : "
+ money + "元,剩余 : " + balance + " 元 ");
}
}
}
lock锁:
|ock是显示锁,需要手动开启和关闭synchronized是 隐式锁,自动开启执行完自动关闭
lock只有代码块锁,而synchronized支持方法和代码块锁
lock锁,需要JVM花费较少的时间来进行资源调度.性能相对较好,而有很好的扩展性 使用顺序: Lock锁--->同步代码块锁--->方法锁
public class Lock_ {
public static void main(String[] args) {
// 创建账户,余额为3000
Account1 act = new Account1(3000);
// 两个线程,每个取1000
Thread t1 = new Processor_09(act);
Thread t2 = new Processor_09(act);
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
}
// 线程类
class Processor_09 extends Thread {
// 账户
Account1 act;
public Processor_09(Account1 act) {
this.act = act;
}
@Override
public void run() {
// 取1000
act.withDraw(1000);
}
}
// 实体类
class Account1 {
// 余额
private double balance;
// 创建锁对象
Lock lock = new ReentrantLock();
public void withDraw(double money) {
System.out.println(Thread.currentThread().getName() + " 进来了");
// 开始同步
lock.lock();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 语句块锁
// 余额减去取钱金额
double after = balance - money;
// 新余额复制给余额
balance = after; // 2000
System.out.println(Thread.currentThread().getName() + " 取钱成功,取款 : "
+ money + "元,剩余 : " + balance + " 元 ");
// 解锁
lock.unlock();
}
public Account1(double balance) {
super();
this.balance = balance;
}
public Account1() {
super();
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
定时器:
public class dingShi_ {
public static void main(String[] args) {
// 1要做的事,也就是任务对象
// 2什么时候开始做
// 3间隔时间,每隔多久做一次
Timer t = new Timer();
//任务类,开始时间,持续时长
t.schedule(new data(), 1000 * 6, 1000 * 3);
}
}
// 计划任务
class data extends TimerTask {
@Override
public void run() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
String time = sdf.format(date);
System.out.println(time);
}
}