java之多线程篇

来源:互联网 发布:凯聪智云软件 编辑:程序博客网 时间:2024/06/03 16:34

java多线程做为java语言的一个特性,小伙伴们是不是面试的时候经常被问到,今天我们就来总结一下多线程

  线程的概念:
         一个程序运行的过程被称为进程,线程相当于进程的执行流,线程之间共享内存资源,在相互切换时消耗资源小,可以 说   线程是轻量级的进程;
  适用场合:线程适用于需要多任务执行的程序,如并发访问等;
===================================================================================
  多线程实现方法
    (1)继承Thread类,重写run方法
               

package test1;

 

public class TestThreadextends Thread{

     int i=0;

 public static void main(String[] args) {

   TestThread thread = new TestThread();

       thread.start();

  }

 @Override

 public void run()

 {

 while(i<9)

{    i++;

     try {

Thread.sleep(1000);

System.out.println("线程执行种。。。");//sleep抛出中断异常

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

 }

}

===============================================================================
    (2)实现Runnable接口,重run方法

package test1;

public class TestRunnableimplements Runnable {

int i=0;

public static void main(String[] args) {

    TestRunnable runnable = new TestRunnable();

    Thread  thread = new Thread(runnable);

    thread.start();

}

@Override

public void run() {

while(i<10)

{

   try{

   i++;

   Thread.sleep(1000);//sleep抛出中断异常

   System.out.println("线程执行中。。");

   }catch (Exception e) {

e.printStackTrace();

    }

  }

 }

}

  线程常用方法:
    (1)Thread.currentThread()  获取当前代码片段的线程
             getId();获得线程Id;
             getName();获得线程Name
             getPriority();返回线程优先级
             getState();返回线程状态
             isAlive();测试线程是否在安全状态
             isinterrupted();线程是否中断
             setPriorty(int priority);设置线程优先级,优先级选择区间[0,10];
    (2)Thread.sleep(long ms);让线程进入休眠,休眠时间ms;
     

package test1;

 

public class TestRunnableimplements Runnable {

int i=0;

public static void main(String[] args) {

    TestRunnable runnable = new TestRunnable();

    Thread  thread = new Thread(runnable);

    thread.start();

    System.out.println("线程id="+thread.getId());

    System.out.println("线程名"+thread.getName());

    System.out.println("线程优先级"+thread.getPriority());

    System.out.println("线程当前状态"+thread.getState());

    System.out.println("线程安全状态"+thread.isAlive());

    System.out.println("线程是否中断"+thread.isInterrupted());

    thread.setPriority(9);

    System.out.println("线程优先级"+thread.getPriority());

}

@Override

public void run() {

while(i<10)

{

   try{

   i++;

   Thread.sleep(1000);//sleep抛出中断异常

   System.out.println("线程执行中。。");

   System.out.println("当前线程"+Thread.currentThread().getName());

   }catch (Exception e) {

e.printStackTrace();

}

}

}

}

================================================================================ 

 解决线程并发的安全方法  

public class TestRunnable implements Runnable {

 

public static void main(String[] args) {

    TestRunnable runnable = new TestRunnable();

    Thread  thread1 = new Thread(runnable,"thread01");

    Thread  thread2 = new Thread(runnable,"thread02");

    thread1.start();

    thread2.start();

}

@Override

public void run() {

for(int i=0;i<3;i++)

{

   try{

   Thread.sleep(1000);//sleep抛出中断异常

   System.out.println("当前线程"+Thread.currentThread().getName());

   }catch (Exception e) {

e.printStackTrace();

}

}

}

}

运行结果

当前线程thread02

当前线程thread01

当前线程thread02

当前线程thread01

当前线程thread02

当前线程thread01

  
     使用synchronized关键字
     第一种使用synchronized修饰代码块

    

package test1;

public class TestRunnableimplements Runnable {

public static void main(String[] args) {

    TestRunnable runnable = new TestRunnable();

    Thread  thread1 = new Thread(runnable,"thread01");

    Thread  thread2 = new Thread(runnable,"thread02");

    thread1.start();

    thread2.start();

}

@Override

public void run() {

  synchronized (this) {

for(int i=0;i<3;i++)

{

   try{

   Thread.sleep(1000);//sleep抛出中断异常

   System.out.println("当前线程"+Thread.currentThread().getName());

   }catch (Exception e) {

e.printStackTrace();

}

}

 }

}

}

运行结果

当前线程thread01

当前线程thread01

当前线程thread01

当前线程thread02

当前线程thread02

当前线程thread02

      第二种使用synchronozed修饰run方法         
     

package test1;

 

public class TestRunnableimplements Runnable {

public static void main(String[] args) {

    TestRunnable runnable = new TestRunnable();

    Thread  thread1 = new Thread(runnable,"thread01");

    Thread  thread2 = new Thread(runnable,"thread02");

    thread1.start();

    thread2.start();

}

@Override

public synchronized void run() {

  synchronized (this) {

for(int i=0;i<3;i++)

{

   try{

   Thread.sleep(1000);//sleep抛出中断异常

   System.out.println("当前线程"+Thread.currentThread().getName());

   }catch (Exception e) {

e.printStackTrace();

}

}

}

}

}

==================================================================================
  协调线程之间的工作wait()和notify()方法;

  wait()和notify()的协调机制可以通过锁定和唤醒线程的方式解决线程的并发问题;