正在运行的程序,称为进程,一个应用程序在内存中占用的资源才是进程
线程:是进程中的某-功能开启了一条让CPU来执行路径,路径就是线程
线程就是一条路,CPU去内存中取出数据执行
每个线程都是具有优先级,优先级越高的线程,相对的会占用CPU的资源会更多,每个线程都是采用默认的优先级,轮流使用CPU资源,平均分配
设置线程的优先级,抢占
线程也是对象,对象都是由类创建
java.lang.Thread
类,线程对象类
实现步骤:
public class MyThread extends Thread{
public void run() {
for(inti=0;i<100;i++){
System. out. print1n("子类重写方法run..."+i);
}
}
}
public static void main(string[] args) {
//创建Thread类的子类对象
MyThread my = new MyThread();
//对象调用方法start()启动线程
my. start();
for(int i = 0 ;i< 100; i++) {
system. out. print1n("main方法... "+i);
}
}
每次启动一个新的线程,就会创建出新的栈内存,运行方法run()
开启的线程越多,内存消耗越大,栈内存(线程栈,私有)
实现步骤:
public class MyRunnable implements Runnable {
pub1ic void run() {
for(inti= 0 ;i< 100;1++) {
System. out. print1n("实现类重写方法run..."+i);
}
}
}
public static void main(String[] args) {
Runnable r = new MyRunnable();
//创建Thread类对象,传递Runnable接口实现类对象
Thread thread = new Thread(r) ;
//Thread类方法start()启动线程
thread. start( );
for(int i=0;i<100;i++){
System. out. print1n("main..."+i) ;
}
}
每个线程都有自己的名字,默认的
Thread类,方法String getName()
线程的默认名字是:Thread-0 , Thread-1
public class MyThread extends Thread{
public void run() {
//直接调用父类的方法getName()
System. out. println("继承重写方法. ."+super . getName());
}
}
public static void main(String[] args) {
MyThread my = new MyThread();
my. start();
}
Thread类方法:
static Thread currentThread()
返回当前线程对象Thread t = Thread. currentThread();
获得线程名的通用方式public static void main(String[] args) {
//使用静态方法,获取当前线程对象
Thread t = Thread. currentThread();
System. out. println(t . getName()) ;
MyThread my = new MyThread();
my. start();
}
Thread类,方法setName(String 线程名)
不建议修改线程名
开发的是多线程的程序,如果出现了线程安全问题,程序人员是最大的责任人
安全问题产生:必须是多线程程序,争夺同一个资源
票源是固定值,常量,网络售票,现场自动售票,人工窗口
现场有3个人工窗口,同时售票
/*
*售票案例,引出线程的安全问题
*3个窗口同时售票工作,票是固定
*3个窗口看成三个线程,抢夺资源
*/
public class Ticket implements Runnable {
//定 义100张
private int tickets = 100;
//线程调用的方法,售票工作
public void run() {
//票数是0的时候,停止
while (true) {
if(tickets > 0) {
System. out . println("出售第"+tickets-- +"张");
}
}
}
}
public static void main(String[] args) {
//创建接口实现类对象
Ticket ticket = new Ticket();
//创建3个线程
Thread t0 = new Thread( ticket );
Thread t1 = new Thread( ticket );
Thread t2 = new Thread( ticket );
//启动线程
t0.start();
t1.start();
t2.start();
}
public class Ticket implements Runnable {
//定 义100张
private int tickets = 100;
//线程调用的方法,售票工作.
public void run() {
// 票数是o的时候,停止了
while(true) {
if(tickets > 0) {
//线程休眠, Thread类的静态方法sleep
try {
Thread. sleep(5);
}catch(Exception ex) {}
System. out. print1n("出售第"+tickets-- +"张");
}
}
}
}
思想:当一个线程没有完成售票工作之前,其他的线程不能参与买票
同步技术:保证线程安全的解决办法
synchronized (任意对象){
写的是线程操作的所有共享资源
}
同步代码块
public static void main(String[] args) {
//创建接口实现类对象
Ticket ticket = new Ticket();
//创建3个线程
Thread t0 = new Thread( ticket );
Thread t1 = new Thread( ticket );
Thread t2 = new Thread( ticket );
//启动线程
t0.start();
t1.start();
t2.start();
}
public class Ticket implements Runnable {
//定 义100张
private int tickets = 100;
private Object obj = new Object();
//线程调用的方法,售票工作
public void run() {
//票数是0的时候,停止了
while(true) {
synchronized( obj ) {
if(tickets > 0) {
//线程休眠, Thread类的静态方法s1eep
try {
Thread. sleep(5);
}catch(Exception ex) {}
System. out. println(Thread. currentThread() . getName()+ "出售第"+tickets-- +"张");
}
}
}
}
}
就是把对象锁(唯一性)
线程判断锁,获取锁,释放锁,一旦线程安全,运行速度下降,必须牺牲速度,保证数据安全性
public class Ticket implements Runnable {
//定义100张
private int tickets = 100;
private Object obj=new Object();
public void run() {
//调用售票的方法
while(true) {
saleTicket();
}
}
//定义方法,专门处理售票tickets--
public void saleTicket() {
synchronized( obj ) {
if(tickets > 0) {
//线程休眠, Thread类的静态方法s1eep
try {
Thread. sleep(5);
}catch(Exception ex) {}
System. out. println(Thread. currentThread() . getName()+ "出售第"+tickets-- +"张");
}
}
}
}
public static void main(String[] args) {
//创建接口实现类对象
Ticket ticket = new Ticket();
//创建3个线程
Thread t0 = new Thread( ticket );
Thread t1 = new Thread( ticket );
Thread t2 = new Thread( ticket );
//启动线程
t0.start();
t1.start();
t2.start();
}
方法saleTicket()出现了一个情况,整个的方法里面,所有程序都是线程操作的共享数据,不再使用同步代码块,可以同步整个方法
public class Ticket implements Runnable {
//定义100张
private int tickets = 100;
public void run() {
//调用售票的方法
while(true) {
saleTicket();
}
}
//定义方法,专门处理售票tickets--
public synchronized void saleTicket() {
if(tickets > 0) {
//线程休眠, Thread类的静态方法s1eep
try {
Thread. sleep(5);
}catch(Exception ex) {}
System. out. println(Thread. currentThread() . getName()+ "出售第"+tickets-- +"张");
}
}
}
public static void main(String[] args) {
//创建接口实现类对象
Ticket ticket = new Ticket();
//创建3个线程
Thread t0 = new Thread( ticket );
Thread t1 = new Thread( ticket );
Thread t2 = new Thread( ticket );
//启动线程
t0.start();
t1.start();
t2.start();
}
同步方法中也存在对象锁吗
同步方法中的锁,是当前对象的引用:this
静态的同步方法中,锁肯定不是this
是当前类的对象, Ticket.class文件也是对象
静态的同步方法中,锁对象是当前类的class文件对象
任何一个数据类型,都会有一个静态的属性,属性的名字是class
public static synchronized void saleTicket() {
if(tickets > 0) {
//线程休眠, Thread类的静态方法sleep
try {
Thread.sleep(5);
}catch(Exception ex) {}
System. out. print1n(Thread. currentThread() . getName()+ "出售第"+tickets--
+"张");
}
}
多个线程,在争抢同一个锁对象的时候,造成程序的假死现象
public class LockA{
public static LockA Locka=new LockA();
}
public class LockB{
public static LockB Lockb=new LockB();
}
public class DeadLock implements Runnable{
//定义成员变量,控制线程的走向
private boolean flag;
public DeadLock(boolean flag) {
this.flag =flag;
}
/*
*实现死锁程序
*/
public void run() {
while(true) {
//判断flag=true
//线程先进去A锁同步,在进入B锁同步
if(flag) {
synchronized ( LockA.locka ) {
System. out. println("if. .locka锁");
synchronized( LockB.lockb ) {
System. out. println("if. .lockb锁");
}
}
}
else {
synchronized ( LockB.lockb ) {
System. out. println("if. .locka锁");
synchronized( LockA.locka ) {
System. out. println("if. .locka锁");
}
}
}
}
}
}
public static void main(String[] args) {
//创建 接口Runnable实现类,成员变量设置为true
DeadLock d1 = new DeadLock(true);
//创建接口Runnable实现类,成员变量设置为false
DeadLock d2 = new DeadLock(false);
new Thread(d1).start();
new Thread(d2) . start();
}
/*
*匿名内部类实现线程
*/
public static void main(String[] args) {
/*Runnable r = new Runnable() {
public void run() {
System. out.println("线程开启");
}
};
new Thread(r). start();
*/
new Thread( new Runnable() {
public void run() {
System. out. println("线程开启");
}
} ). start();
new Thread() {
public void run() {
System. . out. println("子类继承父类" );
}
}. start();
}
JDK1.5出现的接口
java.util.concurrent.locks.Lock
替代synchronized,更加灵活
void lock()
获取锁void unlock()
释放锁/*
*售票案例,引出线程的安全问题
*3个窗口同时售票工作,票是固定
*3个窗口看成三个线程,抢夺资源
*/
public class Ticket implements Runnable {
//定 义100张
private int tickets = 100;
//创建Lock接口实现类对象
private Lock lock= new ReentrantLock();
//线程调用的方法,售票工作
public void run() {
//票数是0的时候,停止
while (true) {
//获取锁
lock.1ock();
if(tickets > 0) {
//线程休眠, Thread类的静态方法sleep
try{
Thread. sleep(5);
}catch(Exception ex) {}
System. out . println(Thread . current Thread() . getName()+ " 出售第" +tickets-- +"张");
//释放锁
}
lock . unlock();
}
}
}
public static void main(String[] args) {
//创建接口实现类对象
Ticket ticket = new Ticket();
//创建3个线程
Thread t0 = new Thread( ticket );
Thread t1 = new Thread( ticket );
Thread t2 = new Thread( ticket );
//启动线程
t0.start();
t1.start();
t2.start();
}
线程在她的一生中,创建到死亡,经过多个状态
线程在某一个时刻,只能处于一种状态
共有6种状态
本文地址:https://blog.csdn.net/qq_45018290/article/details/107629112
如对本文有疑问, 点击进行留言回复!!
Android 4.0使用Kotlin调用C语言以及汇编语言
Java Class.forName()用法和newInstance()方法原理解析
网友评论