[关闭]
@liruiyi962464 2017-03-22T03:42:31.000000Z 字数 6849 阅读 437

线程

java

  • 进程里边有多个线程
  • CPU在同一个时间点上只有一个线程在工作
  • CPU只管理资源,线程的进入是随机的
  • 共享
  • 并发
  • 随机

继承Therad类创建多线程

  • 普通java类继承自Therad类,就成为一个线程类,并且可以通过该类的start()方法来启动线程
  • Therad类的子类可以直接实例化,但是要从写run()方法
  1. public class Test1_Therad {
  2. public static void main(String[] args) {
  3. MyThread mt = new MyThread();
  4. mt.start();
  5. for(int i = 0;i<5;i++){
  6. System.out.println("主方法。");
  7. }
  8. //匿名内部类相当于父类的子类
  9. new Thread(){
  10. public void run(){
  11. for(int i = 0;i<5;i++){
  12. try {
  13. sleep(1000);
  14. } catch (InterruptedException e) {
  15. // TODO Auto-generated catch block
  16. e.printStackTrace();
  17. }
  18. System.out.println("匿名内部类");
  19. }
  20. }
  21. }.start();
  22. //new.MyThread().start();
  23. }
  24. }
  25. class MyThread extends Thread{
  26. @Override
  27. public void run() {
  28. // TODO Auto-generated method stub
  29. for(int i = 0;i<5;i++){
  30. try {
  31. sleep(1000);
  32. } catch (InterruptedException e) {
  33. // TODO Auto-generated catch block
  34. e.printStackTrace();
  35. }
  36. System.out.println("先定一个小目标123456789");
  37. }
  38. super.run();
  39. }
  40. }
  1. public class Task3_3Thread {
  2. public static void main(String[] args) {
  3. TrainThread tt1 = new TrainThread();
  4. tt1.setName("火车1");
  5. tt1.start();
  6. }
  7. }
  8. class TrainThread extends Thread{
  9. @Override
  10. public synchronized void run() {
  11. // TODO Auto-generated method stub
  12. synchronized (TrainThread.class) {
  13. System.out.println(Thread.currentThread().getName()+"通过山洞");
  14. try {
  15. Thread.sleep(2000);
  16. } catch (InterruptedException e) {
  17. // TODO Auto-generated catch block
  18. e.printStackTrace();
  19. }
  20. }
  21. }
  22. }

实现接口Runnable创建多线程

  • 实现Runnable接口必须借助Thread类才能创建线程
    1.创建Runnable接口的类的实例
    2.创建一个Thread对象,将第一步实例化得到的Runnable对象作为参数传入Thread类的构造犯法
  • 通过Thread类的start()方法启动线程
  1. public class Test2_Runnable {
  2. public static void main(String[] args) {
  3. MyRunnable mr = new MyRunnable();
  4. Thread t = new Thread(mr);
  5. t.start();
  6. }
  7. }
  8. class MyRunnable implements Runnable{
  9. @Override
  10. public void run() {
  11. // TODO Auto-generated method stub
  12. for(int i = 0;i<5;i++){
  13. try {
  14. Thread.sleep(1000);
  15. } catch (InterruptedException e) {
  16. // TODO Auto-generated catch block
  17. e.printStackTrace();
  18. }
  19. System.out.println("Runnable");
  20. }
  21. }
  22. }
  1. public class Task3_3 {
  2. public static void main(String[] args) {
  3. TrainThread tt = new TrainThread();
  4. Thread t1 = new Thread(tt, "火车1");
  5. t1.start();
  6. }
  7. }
  8. class TrainThread implements Runnable{
  9. @Override
  10. public synchronized void run() {
  11. // TODO Auto-generated method stub
  12. System.out.println(Thread.currentThread().getName()+"通过山洞");
  13. try {
  14. Thread.sleep(2000);
  15. } catch (InterruptedException e) {
  16. // TODO Auto-generated catch block
  17. e.printStackTrace();
  18. }
  19. }
  20. }

线程常用方法

  1. public class Test3_ThreadFiled {
  2. public static void main(String[] args) {
  3. new Thread("线程a"){
  4. public void run() {
  5. System.out.println(getName()+"........1..aaa...."+getId());
  6. }
  7. }.start();
  8. new Thread("线程b"){
  9. public void run() {
  10. System.out.println(getName()+".........2...bbb.."+getId());
  11. }
  12. }.start();
  13. new Thread(new Runnable() {
  14. @Override
  15. public void run() {
  16. // TODO Auto-generated method stub
  17. System.out.println("没有设置/获取名字Runnable......3........."+Thread.currentThread().getId());
  18. //currentThread()返回当前线程的引用
  19. //Thread.currentThread().setName("线程c");设置线程的名字
  20. System.out.println(Thread.currentThread().getName()+":设置/获取名字........4...Runnable......"+Thread.currentThread().getId());
  21. }
  22. },"线程c").start();//在这里也可以线程名字
  23. Thread.currentThread().setName("我是主线程");
  24. System.out.println(Thread.currentThread().getName()+"...........5............"+Thread.currentThread().getId());
  25. }
  26. }

线程同步

  • 直接在方法中添加关键字也可以加锁synchronized
  • synchronized运行完之后再换/加锁
  1. package com.xianchengtongbu;
  2. public class Test_Synchronized {
  3. public static void main(String[] args) {
  4. Printer p = new Printer();
  5. new Thread(){
  6. public void run() {
  7. while(true){
  8. p.print1();
  9. }};
  10. }.start();
  11. new Thread(){
  12. public void run() {
  13. while(true){
  14. p.print2();
  15. }
  16. };}.start();
  17. }
  18. }
  19. class Printer{
  20. Object obg = new Object();
  21. //直接在方法中添加关键字也可以加锁synchronized
  22. public void print1() {//线程同步
  23. //如果同步非静态方法,锁对象是this
  24. //如果同步静态方法,锁对象是.class
  25. synchronized (obg) {//synchronized运行完之后再换/加锁
  26. System.out.print("每");
  27. System.out.print("天");
  28. System.out.print("进");
  29. System.out.print("步");
  30. System.out.print("一");
  31. System.out.print("点");
  32. System.out.print("点");
  33. System.out.println();
  34. }
  35. }//锁对象是this
  36. public void print2() {//线程不同步
  37. System.out.print("好");
  38. System.out.print("好");
  39. System.out.print("学");
  40. System.out.print("习");
  41. System.out.println();
  42. }
  43. }

死锁

  • 产生的原因:俩个线程都需要同一个对象,这个互相等待对方释放锁的过程,会导致死锁
  1. public class Test1_SiSuo {
  2. // 创建俩个筷子
  3. private static String s1 = "筷子左";
  4. private static String s2 = "筷子右";
  5. public static void main(String[] args) {
  6. // 匿名内部类相当于父类的子类
  7. new Thread("皮皮虾") {
  8. @Override
  9. public void run() {
  10. // TODO Auto-generated method stub
  11. while (true) {
  12. synchronized (s1) {
  13. System.out.println(this.getName()+"获得"+s1+"等待"+s2);
  14. synchronized (s2) {
  15. System.out.println(this.getName()+"获得"+s2);
  16. }
  17. }
  18. }
  19. }
  20. }.start();
  21. new Thread("PPD") {
  22. @Override
  23. public void run() {
  24. // TODO Auto-generated method stub
  25. while (true) {
  26. synchronized (s2) {
  27. System.out.println(this.getName()+"获得"+s2+"等待"+s1);
  28. synchronized (s1) {
  29. System.out.println(this.getName()+"获得"+s1);
  30. }
  31. }
  32. }
  33. }
  34. }.start();
  35. }
  36. }

线程同步

  • wait(): 在其他线程调用此对象的 notify() 方法或 notifyAll() 方法前,导致当前线程等待。
  • notify():唤醒在此对象监视器上等待的单个线程。
  • notifyAll(): 唤醒在此对象监视器上等待的所有线程。

俩个通信

  1. public class Test1_Tongxin {
  2. public static void main(String[] args) {
  3. Printer p = new Printer();
  4. new Thread(){
  5. @Override
  6. public void run() {
  7. // TODO Auto-generated method stub
  8. while(true){
  9. try {
  10. sleep(1000);
  11. p.print1();
  12. } catch (Exception e) {
  13. // TODO Auto-generated catch block
  14. e.printStackTrace();
  15. }
  16. }
  17. }
  18. }.start();
  19. new Thread(){
  20. @Override
  21. public void run() {
  22. // TODO Auto-generated method stub
  23. while(true){
  24. try {
  25. sleep(1000);
  26. p.print2();
  27. } catch (Exception e) {
  28. // TODO Auto-generated catch block
  29. e.printStackTrace();
  30. }
  31. }
  32. }
  33. }.start();
  34. }
  35. }
  36. class Printer{
  37. private int n = 1;
  38. public void print1() throws Exception{
  39. synchronized (this) {
  40. if(n!=1){
  41. this.wait();//等待
  42. }
  43. System.out.print("每");
  44. System.out.print("天");
  45. System.out.print("进");
  46. System.out.print("步");
  47. System.out.print("一");
  48. System.out.print("点");
  49. System.out.print("点");
  50. System.out.println();
  51. n = 2;
  52. this.notify();//唤醒
  53. }
  54. }
  55. public void print2() throws Exception{
  56. synchronized (this) {
  57. if(n!=2){
  58. this.wait();
  59. }
  60. System.out.print("好");
  61. System.out.print("好");
  62. System.out.print("学");
  63. System.out.print("习");
  64. System.out.println();
  65. n = 1;
  66. this.notify();
  67. }
  68. }
  69. }

多个通信

  1. public class Test1_Tongxin {
  2. public static void main(String[] args) {
  3. Printer p = new Printer();
  4. new Thread(){
  5. @Override
  6. public void run() {
  7. // TODO Auto-generated method stub
  8. while(true){
  9. try {
  10. sleep(1000);
  11. p.print1();
  12. } catch (Exception e) {
  13. // TODO Auto-generated catch block
  14. e.printStackTrace();
  15. }
  16. }
  17. }
  18. }.start();
  19. new Thread(){
  20. @Override
  21. public void run() {
  22. // TODO Auto-generated method stub
  23. while(true){
  24. try {
  25. sleep(1000);
  26. p.print2();
  27. } catch (Exception e) {
  28. // TODO Auto-generated catch block
  29. e.printStackTrace();
  30. }
  31. }
  32. }
  33. }.start();
  34. new Thread(){
  35. @Override
  36. public void run() {
  37. // TODO Auto-generated method stub
  38. while(true){
  39. try {
  40. sleep(1000);
  41. p.print3();
  42. } catch (Exception e) {
  43. // TODO Auto-generated catch block
  44. e.printStackTrace();
  45. }
  46. }
  47. }
  48. }.start();
  49. }
  50. }
  51. class Printer{
  52. private int n = 1;
  53. public void print1() throws Exception{
  54. synchronized (this) {
  55. while(n!=1){
  56. this.wait();//等待
  57. }
  58. System.out.print("每");
  59. System.out.print("天");
  60. System.out.print("进");
  61. System.out.print("步");
  62. System.out.print("一");
  63. System.out.print("点");
  64. System.out.print("点");
  65. System.out.println();
  66. n = 2;
  67. this.notifyAll();//唤醒其它线程
  68. }
  69. }
  70. public void print2() throws Exception{
  71. synchronized (this) {
  72. while(n!=2){
  73. this.wait();//等待
  74. }
  75. System.out.print("好");
  76. System.out.print("好");
  77. System.out.print("学");
  78. System.out.print("习");
  79. System.out.println();
  80. n = 3;
  81. this.notifyAll();//唤醒其它线程
  82. }
  83. }
  84. public void print3() throws Exception{
  85. synchronized (this) {
  86. while(n!=3){
  87. this.wait();//等待
  88. }
  89. System.out.print("天");
  90. System.out.print("天");
  91. System.out.print("向");
  92. System.out.print("上");
  93. System.out.println();
  94. n = 1;
  95. this.notifyAll();//唤醒其它线程
  96. }
  97. }
  98. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注