码迷,mamicode.com
首页 > 编程语言 > 详细

java多线程技能

时间:2017-05-05 00:57:58      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:play   技术分享   操作   影响   one   value   on()   i++   进程和线程   

1.进程和线程的定义

  进程:受操作系统管理的基本运行单元

  线程:进程中独立运行的子任务

2.使用多线程

  2.1继承Thread类:自定义线程类并继承Thread类,并且重写run方法。

技术分享
class MyThread extends Thread
{    
    private int count=1000;
    @Override
    public void run() {
        // TODO Auto-generated method stub
        for(int i=0;i<500;i++){
            count--;
            System.out.println(Thread.currentThread().getId()+":"+count);    
        }
        
    }
}

public class Test {
    /**
     * @param args
     * @throws ClassNotFoundException 
     * @throws IOException 
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MyThread myThread=new MyThread();
        for(int i=0;i<2;i++){
            Thread thread=new Thread(myThread);
            thread.start();
        }
        
    
    }

}
View Code

  Thread类start()方法其实就是开始接受由操作系统分配的时间片,一但当前线程接受到时间片就开始执行run()方法

  2.2实现Runnable接口(推荐)

技术分享
class MyThread implements Runnable
{    
    private int count=1000;
    @Override
    public void run() {
        // TODO Auto-generated method stub
        for(int i=0;i<500;i++){
            count--;
            System.out.println(Thread.currentThread().getId()+":"+count);    
        }
        
    }
}

public class Test {
    public static void main(String[] args) throws ClassNotFoundException, IOException {
        // TODO Auto-generated method stub
        MyThread myThread=new MyThread();
        for(int i=0;i<2;i++){
            Thread thread=new Thread(myThread);
            thread.start();
        }    
    }
}
View Code

3.实例变量和线程安全

  3.1不共享数据的情况

技术分享
class MyThread extends Thread
{    
    private int count=10;
    @Override
    public void run() {
        // TODO Auto-generated method stub
        for(int i=0;i<5;i++){
            count--;
            System.out.println(Thread.currentThread().getId()+":"+count);    
        }
        
    }
}

public class Test {
    public static void main(String[] args){
        // TODO Auto-generated method stub        
        for(int i=0;i<2;i++){
            Thread thread=new MyThread();
            thread.start();
        }    
    }
}
View Code  

  3.2共享数据的情况

技术分享
class MyThread extends Thread
{    
    private int count=10;
    @Override
    public void run() {
        // TODO Auto-generated method stub
        for(int i=0;i<5;i++){
            count--;
            System.out.println(Thread.currentThread().getId()+":"+count);    
        }
        
    }
}

public class Test {
    public static void main(String[] args){
        // TODO Auto-generated method stub    
        Thread thread1=new MyThread();
        for(int i=0;i<2;i++){
            Thread thread=new Thread(thread1);
            thread.start();
        }    
    }
}
View Code

  3.3非线程安全:多个线程对同一个对象实例的变量进行操作时,出现值不同步、被其它线程更改进而影响程序的执行。

4.停止线程

  4.1Thread类interrupt()方法中断线程,该方法实质上只是改变了线程的中断状态,所以就算interrupt()方法被调用,

  线程还是会继续执行直至结束。  

技术分享
class MyThread extends Thread
{        
    @Override
    public void run() {
        for(int i=0;i<Integer.MAX_VALUE;i++)
        {
            if(!this.interrupted())
            {
                System.out.println(i);
            }
            else
            {
                System.out.println("当前线程被中断,我要退出");
                break;
            }
        }
        System.out.println("这里是线程被中止后的输出,理论上线程被中止 我不应该被输出");//
    }
}

public class Test {
    public static void main(String[] args){
        // TODO Auto-generated method stub    
        Thread thread1=new Thread(new MyThread());
        thread1.start();
        try {
            Thread.sleep(2000);
            thread1.interrupt();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            System.out.println("main catch");
            e.printStackTrace();
        }                
    }
}
View Code

  运行结果:

   ……  

  328198
  328199
  328200
  328201
  328202
  328203
  328204
  当前线程被中断,我要退出
  这里是线程被中止后的输出,理论上线程被中止 我不应该被输出

  4.2异常法中止线程:获取到当前线程的中止状态为true后抛出InterruptedException异常

技术分享
class MyThread extends Thread
{        
    @Override
    public void run() {
        try
        {
            for(int i=0;i<Integer.MAX_VALUE;i++)
            {
                if(!this.interrupted())
                {
                    System.out.println(i);
                }
                else
                {
                    System.out.println("当前线程被中断,我要退出");
                    throw new InterruptedException();
                }
            }
            System.out.println("这里是线程被中止后的输出,理论上线程被中止 我不应该被输出");//
        }
        catch(Exception e)
        {
            System.out.println("进入到异常");//
        }

    }
}

public class Test {
    public static void main(String[] args){
        // TODO Auto-generated method stub    
        Thread thread1=new Thread(new MyThread());
        thread1.start();
        try {
            Thread.sleep(2000);            
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            System.out.println("main catch");
            e.printStackTrace();
        }        
        thread1.interrupt();    
        
    }
}
View Code

  4.3使用return 中止线程:

技术分享
class MyThread extends Thread
{        
    @Override
    public void run() {
        for(int i=0;i<Integer.MAX_VALUE;i++)
        {
            if(!this.interrupted())
            {
                System.out.println(i);
            }
            else
            {
                System.out.println("当前线程被中断,我要退出");
                return;
            }
        }
        System.out.println("这里是线程被中止后的输出,理论上线程被中止 我不应该被输出");//        
    }
}

public class Test {
    public static void main(String[] args){
        // TODO Auto-generated method stub    
        Thread thread1=new Thread(new MyThread());
        thread1.start();
        try {
            Thread.sleep(2000);            
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            System.out.println("main catch");
            e.printStackTrace();
        }        
        thread1.interrupt();    
        
    }
}
View Code

 

  

  

java多线程技能

标签:play   技术分享   操作   影响   one   value   on()   i++   进程和线程   

原文地址:http://www.cnblogs.com/liandy0906/p/6810639.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!