java中的线程、进程、锁

本文介绍了Java中的线程、进程概念,强调了多线程并发可能导致的数据错误问题,并详细阐述了线程的创建、状态转换以及常用方法。通过实例说明了线程同步的重要性,特别是使用`synchronized`关键字实现的同步代码块和同步方法,以防止资源竞争导致的错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

加了synchronized就是代表这个是锁,保证每次只能有一个线程访问这个代码,防止多线程并发,出现获取数据错误。当我们运行一个项目进程时会有多个线程启动。一个函数可能由多个线程会来访问,获取其中的公共资源,就是并发,假如一个函数里面有公共资源b=12;线程A把b改为10,而同时线程B来获取数据时,不知道自己的数据被改了,就获取到修改后的数据。这当然不是我们想要的。将函数加一个锁就可以每次只让一个线程来来访问,其他的都是等待


public static synchronized String genUniqueKey() {
        Random random = new Random();
        int num = random.nextInt(900000) + 100000;
        return System.currentTimeMillis() + String.valueOf(num);
    }
如果查看JAVA源代码的时候有native关键字时,就代表是java底层c语言

1.单线程:程序中只存在一个线程,实际主方法就是一个主线程

2.多线程:在一个程序中运行多个任务

3.线程的实现:

1)继承Thread类:必须重写run()方法,等待CPU进行调度

2)实现Runnable接口

4线程的状态

1)创建状态:建立一个多线程对象

2)就绪状态:调用start()方法

3)运行状态:执行run()方法

4)阻塞状态:暂停执行进即将资源给其他线程使用

5)终止状态(死亡状态):线程销毁

5线程的常用方法

1)取得线程的名字:getName();

2)取得当前线程对象:currentThread()

3)判断线程是否启动:isAlive()

4)线程的强行运行:join()

5)线程更的休眠:sleep();

 class RunnableDemo implements Runnable{
	 private String name;
	 public RunnableDemo(String name){
		 this.name=name;
	 }
	  public void run(){
			 for(int i=0;i<50;i++){
				 // System.out.println("A"+Thread.currentThread().getName()); 
				 try{
					 
					 Thread.sleep(1000);//一秒执行一次
					 
				 }
				 catch(InterruptedException e){
					 e.printStackTrace();
				 }
				 System.out.println(name+":"+i);
			 }
		 }
 }
 public class Operation {
	 public static void main(String args[]){
		RunnableDemo r=new RunnableDemo("A");
		// RunnableDemo r1=new RunnableDemo("A");
		Thread t=new Thread(r);
		// Thread t1=new Thread(r1);//第二个线程创建于第一个线程并发执行
		// t1.start();
		t.start();//线程就绪状态
		/* for(int i=0;i<50;i++){
			if(i>10){//当主线程执行10次
				try{
					t.join();//强制让自己的线程执行,之后执行余下的主线程
				}
				catch(InterruptedException e){
					e.printStackTrace();
				}
			}
			System.out.println("主线程:"+i);
		} */
	
	 }
		 
 }
 
 
线程的礼让

 class RunnableDemo implements Runnable{
	 private String name;
	 public RunnableDemo(String name){
		 this.name=name;
	 }
	  public void run(){
			 for(int i=0;i<50;i++){
				 System.out.println(name+":"+i);
				 if(i==10){ //当A执行10次时,执行B十次
					 System.out.println("礼让");
					 Thread.yield();
				 }
			 }
		 }
 }
 public class Operation {
	 public static void main(String args[]){
		RunnableDemo r=new RunnableDemo("A");
		RunnableDemo r1=new RunnableDemo("B");
		Thread t=new Thread(r);
		Thread t1=new Thread(r1);
		t.start();
		t1.start();//线程就绪状态
	
	 }
		 
 }
 
 

6线程的优先级

 class ThRun implements Runnable{
	public void run(){
		for(int i=0;i<5;i++){
			try{
				Thread.sleep(1000);
				System.out.println(Thread.currentThread().getName()+":"+i);
			}
			catch(InterruptedException e){
				e.printStackTrace();
			}
			
		}
	}
 }
 public class Operation {
	 public static void main(String args[]){
		Thread t1=new Thread(new ThRun(),"A");
		Thread t2=new Thread(new ThRun(),"B");
		Thread t3=new Thread(new ThRun(),"C");
		t1.setPriority(Thread.MIN_PRIORITY);
		t2.setPriority(Thread.NORM_PRIORITY);
		t3.setPriority(Thread.MAX_PRIORITY);//不一定它大,就先执行,只是优先抢CPU
		t1.start();
		t2.start();//线程就绪状态
		t3.start();
	
	 }
		 
 }
 
 
7 线程同步

有五张票,同时由三个窗口来买,卖票的等待时间一秒。

 class ThRun implements Runnable{
	private int ticket=5;
	public void run(){
		for(int i=0;i<10;i++){
			if(ticket>0){
				try{
						Thread.sleep(1000);
				System.out.println("车票:"+ticket--);
				}
				catch(InterruptedException e){
					e.printStackTrace();
				}
			
			}
		}
	}
 }
 public class Operation {
	 public static void main(String args[]){
		ThRun m=new ThRun();
		Thread t1=new Thread(m);
		Thread t2=new Thread(m);
		Thread t3=new Thread(m);
		t1.start();
		t2.start();//线程就绪状态
		t3.start();
	
	 }
		 
 }
 
 



上图出现了负数,证明五张车票没有做到资源共享,就需要用到同步,下面是同步代码块,同步方法就只要在方法类型前面加synchronized

 class ThRun implements Runnable{
	private int ticket=5;
	public void run(){
		for(int i=0;i<10;i++){
			synchronized (this){
				if(ticket>0){
				try{
						Thread.sleep(1000);
				System.out.println("车票:"+ticket--);
				}
				catch(InterruptedException e){
					e.printStackTrace();
				}
			
			}
			}
			
		}
	}
 }
 public class Operation {
	 public static void main(String args[]){
		ThRun m=new ThRun();
		Thread t1=new Thread(m);
		Thread t2=new Thread(m);
		Thread t3=new Thread(m);
		t1.start();
		t2.start();//线程就绪状态
		t3.start();
	
	 }
		 
 }
 
 

8线程的生命周期


锁就是例如:大学毕业要高薪水,企业要经验,形成死锁。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值