java多线程

Posted gali

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java多线程相关的知识,希望对你有一定的参考价值。

1.概念理解

进程:是操作系统结构的基础;是一次程序的执行;是一个程序及其数据在处理机上顺序执行时所发生的活动;是程序在一个数据
集合上运行的过程,它是系统进行资源分配和调度的一个独立单位。
线程:是在进程中独立运行的子任务。
多线程的优点:可以最大限度的利用cpu的空闲时间来处理其他任务。

线程实现:

线程可以通过集成Thread :但是java不支持多继承。
实现Runable接口

注意点:执行start()方法的顺序不代表线程启动的顺序。

 

2:对比数据的共享

 

public class ConThread  extends Thread{

	public int count=5;
	public String name;
	
	public ConThread(String name){
		this.name=name;
		this.setName(name);
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
	      
    if(count>0){
        count--;
        System.out.println("this Thread name is :"+this.currentThread().getName()+"--"+count);


        }


	    
	
		}
	
	
	

}



      ConThread th1=new ConThread("A");
     ConThread th2=new ConThread("B");}}
    ConThread th3=new ConThread("C");
    ConThread th4=new ConThread("D");
	th1.start();
	th2.start();
	th3.start();
	th4.start()

 

  

 

  以上的线程通过newThread之间创建start,数据count并不会共享,每个线程都会输出4,3,2,1,

public class ConThread  extends Thread{

	public int count=5;
	public String name;
	
	public ConThread(){
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
	//	while(count>0){
			count--;
			System.out.println("this Thread name is :"+this.currentThread().getName()+"--"+count);
	
	//}
		}
	


}



		ConThread th=new ConThread();
		Thread th0=new Thread(th,"A");
		Thread th1=new Thread(th,"B");
		Thread th2=new Thread(th,"C");
		th0.start();
		th1.start();
		th2.start();
		

  以上线程则是传入构造方法,创建Thread,则会共享数据count,但是线程非安全。

 

 

3: synchronized 关键字

	public class ConThread  extends Thread{

	public int count=5;
	public String name;
	
	public ConThread(){
	}
	
	@Override
	 synchronized public void run() {
		// TODO Auto-generated method stub
		super.run();
	//	while(count>0){
			count--;
			System.out.println("this Thread name is :"+this.currentThread().getName()+"--"+count);
	
	//}
		}
	
	
	

}

		
		
			
		ConThread th=new ConThread();
		Thread th0=new Thread(th,"A");
		Thread th1=new Thread(th,"B");
		Thread th2=new Thread(th,"C");
		th0.start();
		th1.start();
		th2.start();

 4:currentThread  

        isAlive 判断线程是否存活状态,start 后 

 

public static void main(String[] args) {
		// TODO Auto-generated method stub

		
		
		CountOperate c=new CountOperate();
		Thread th0=new Thread(c,"A");
		Thread th1=new Thread(c,"B");
	//	System.out.println("th0 id :"+th0.getId());
		//System.out.println("main begain th0 is alive :"+th0.isAlive());
	//	th0.setName("A");
		th0.start();
		th1.start();
	//	System.out.println("th0 id :"+th0.getId());
	//	System.out.println("main begain th0 is alive :"+th0.isAlive());
		




public class CountOperate extends Thread {
	
	public CountOperate() {
		
		System.out.println("countOperate --begain");
		System.out.println("Thread.currentThread.getName():"+Thread.currentThread().getName());
		System.out.println("Thread.currentThread.isAlive:"+Thread.currentThread().isAlive());
		System.out.println("this.isAlive:"+this.isAlive());
		System.out.println("this.getName:"+this.getName());
		System.out.println("countOperate --end");
	}
	
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		System.out.println("cunThread.getId:"+this.getId());
		System.out.println("run --begain");
		System.out.println("Thread.currentThread.getName():"+Thread.currentThread().getName());  // currentThread 是执行该代码块的线程
		//System.out.println("Thread.currentThread.isAlive:"+Thread.currentThread().isAlive());
		//System.out.println("this.isAlive:"+this.isAlive());  //  是false 搞不懂
		//System.out.println("cunThread.getId:"+this.getId()); 
          
          // this指当前的线程 CountOperate  该对象传入new Thread ,run 是new Thread 对象调用的 即是 由 A,B 调用
          System.out.println("this.getName:"+this.getName()); 
          System.out.println("run --end");
 } 
}

 

  

 

 打印:

countOperate --begain
Thread.currentThread.getName():main
Thread.currentThread.isAlive:true
this.isAlive:false
this.getName:Thread-0
countOperate --end
cunThread.getId:10
run --begain
Thread.currentThread.getName():A
Thread.currentThread.isAlive:true
this.isAlive:false
cunThread.getId:10
this.getName:Thread-0
run --end
cunThread.getId:10
run --begain
Thread.currentThread.getName():B
Thread.currentThread.isAlive:true
this.isAlive:false
cunThread.getId:10
this.getName:Thread-0
run --end

 5:Sleep()

   在指定毫秒数内让当前"正在执行的线程"休眠,这个线程是指this.currentThread()返回的线程 

 

public class SleepThread extends  Thread{

	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		try {
			System.err.println("run threadName = "+this.currentThread().getName()+" begain");
			Thread.sleep(2000);
			System.err.println("run threadName = "+this.currentThread().getName()+" end");
			
			
		} catch (InterruptedException e) {
			// TODO: handle exception
		}
		
	}
}



	SleepThread th0=new SleepThread();
	System.err.println("begain = "+System.currentTimeMillis());
	th0.run();
	System.err.println("end  = "+System.currentTimeMillis());


输出:
begain = 1521465823061
run threadName = main begain
run threadName = main end
end = 1521465825073

  

th0.run 变成th0.start();
输出:
begain = 1521466181414
end  = 1521466181414
run threadName = Thread-0 begain
run threadName = Thread-0 end

  

    SleepThread th0=new SleepThread();
		Thread th1=new Thread(th0);
		Thread th2=new Thread(th0);
		System.err.println("begain = "+System.currentTimeMillis());
		th2.setName("A");
		th2.start();
		th1.setName("B");
		th1.start();
		System.err.println("end  = "+System.currentTimeMillis());
			
			
begain = 1521466821288
end  = 1521466821288
run threadName = A begain
run threadName = B begain
run threadName = A end
run threadName = B end   // this.currentThread().getName() currentThread指执行该改代码块的线程  和this区别开来

  6:interrupt()  中断线程

 

public class InterruptedThread  extends Thread{
	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		for (int i = 0; i <1000; i++) {
			System.out.println("i = "+(i+1));
		}
	}

}



public class ThreadTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		try {
			InterruptedThread th=new InterruptedThread();
			th.start();
			Thread.sleep(1000);
			th.interrupt();
			System.out.println("是否停止了1--"+th.interrupted());
			System.out.println("是否停止了2--"+th.interrupted());
			
		} catch (InterruptedException e) {
			// TODO: handle exception
		}

	
	}

}

i = 999
i = 1000
是否停止了1--false  //th.interrupt 是指main线程
是否停止了2--false

  

try {
			InterruptedThread th=new InterruptedThread();
			th.start();
			Thread.sleep(1000);
			Thread.currentThread().interrupt();
			System.out.println("是否停止了1--"+Thread.currentThread().interrupted());
			System.out.println("是否停止了2--"+Thread.currentThread().interrupted());
			
		} catch (InterruptedException e) {
			// TODO: handle exception
		}
		
		
		
i = 999
i = 1000
是否停止了1--true
是否停止了2--false  这里是中端了main线程

但是俩次调用interrupted()  interrupted() 判断当前线程是否处于中断状态,线程的中断状态由该方法清除

注意点:

isInterrupted()对比interrupted()没有清除功能

 












以上是关于java多线程的主要内容,如果未能解决你的问题,请参考以下文章

Java多线程与并发库高级应用-工具类介绍

多线程 Thread 线程同步 synchronized

Java多线程具体解释

自己开发的在线视频下载工具,基于Java多线程

什么是JAVA的多线程?

多个用户访问同一段代码