java多线程

如下内容是今日敲的代码,今日所学习的是多线程,在与多线程类似的是多进程,可是多进程基本是进程与进程中很难有交流的,可是多线程容易交流,并且是并行运行(多线程运行),在一个Thread对象里,只能调用一个start方法,须要再加一个线程必须再新建一个对象。今日的学习遇到的问题和经验基本都写在注释里面了。多线程

附代码:ide

 1 package com.wsy.test;  2 
 3 public class Thread1 extends Thread {  4     public void run()  5  {  6         while(true)  7  {  8             
 9 
10             try 
11  { 12                 System.out.println("Hello"); 13                 Thread.sleep(1000); 14             } catch (InterruptedException e) { 15                 // TODO Auto-generated catch block
16  e.printStackTrace(); 17  } 18  } 19  } 20 }
 1 package com.wsy.test;  2 
 3 public class ThreadDemo1 {  4 
 5     public static void main(String[] args) {  6         Thread1 t = new Thread1();  7         new Thread(t).start();  8         new Thread(t).start();  9         new Thread(t).start(); 10         new Thread(t).start(); 11         new Thread(t).start(); 12  } 13 
14 }
 1 package com.wsy.test;  2 
 3 public class RunnableTest1 implements Runnable {  4  @Override  5     public void run()  6  {  7         while(true)  8  {  9             System.out.println("Hello"); 10             try { 11                 Thread.sleep(2000); 12             } catch (InterruptedException e) { 13  e.printStackTrace(); 14  } 15  } 16  } 17 
18     public static void main(String[] args) { 19         //在老师的视频里出现的直接是27行的那个代码,因此我想尝试可否使用这个方法调用start方法 20         //结果编译器回报错,显示start方法未定义 21 // new RunnableTest().start(); 22 // new Thread(new RunnableTest()).run(); 23         //在这里我还遇到了一个错误 我直接调用的run方法,而不是调用的start方法去触发的run方法 24         //因此它一直在执行run方法里的死循环 永远没法执行到main方法里面的代码 25         //通过学习得知当直接调用run方法时,这时候会是串行运行,而使用start方法触发run方法时并行运行 26         //并行运行(多线程运行)
27         new Thread(new RunnableTest1()).start(); 28         while(true) 29  { 30             System.out.println("World"); 31             try { 32                 Thread.sleep(2000); 33             } catch (InterruptedException e) { 34                 // TODO Auto-generated catch block
35  e.printStackTrace(); 36  } 37  } 38  } 39 
40 }
 1 package com.wsy.test;  2 
 3 public class RunnableTest2 implements Runnable {  4  @Override  5     public void run()  6  {  7         while(true)  8  {  9             System.out.println("Hello"); 10             try { 11                 Thread.sleep(2000); 12             } catch (InterruptedException e) { 13  e.printStackTrace(); 14  } 15  } 16  } 17     public static void main(String[] args) { 18         new Thread(new RunnableTest2()).run(); 19         //在这里用于没法执行到21行的循环,由于在run中有个死循环,这是一个串行运行 20         //只有执行完一条语句才能执行下一条语句
21         while(true) 22  { 23             System.out.println("World"); 24             try { 25                 Thread.sleep(2000); 26             } catch (InterruptedException e) { 27                 // TODO Auto-generated catch block
28  e.printStackTrace(); 29  } 30  } 31 
32  } 33 
34 }
 1 package com.wsy.test;  2 
 3 public class RunnableTest3 implements Runnable {  4 
 5  @Override  6     public void run()  7  {  8         while(true)  9  { 10             try 
11  { 12                 System.out.println("Hello"); 13                 Thread.sleep(1000); 14             } catch (InterruptedException e) { 15                 // TODO Auto-generated catch block
16  e.printStackTrace(); 17  } 18  } 19 
20  } 21 
22     public static void main(String[] args) { 23         new Thread(new RunnableTest3()).start(); 24         for(int i =0; i<10; i++) 25  { 26             try 
27  { 28                 System.out.println("World"); 29                 Thread.sleep(1000); 30             } catch (InterruptedException e) { 31                 // TODO Auto-generated catch block
32  e.printStackTrace(); 33  } 34  } 35         System.out.println("main end "); 36  } 37 
38 }
 1 package com.wsy.test;  2 
 3 public class RunnableTest4 {  4 
 5     public static void main(String[] args) {  6         RunnableTest3 tt = new RunnableTest3();  7         //实现了Runnable接口的类没法直接使用start函数,须要去将该类包装成一个Thread类去使用start函数  8 // tt.start();
 9         Thread t = new Thread(tt); 10         t.start();//实际上和RunnableTest3使用start方法同样
11 
12  } 13 
14 }
 1 package com.wsy.test;  2 
 3 public class RunnableTest5 {  4     public static void main()  5  {  6         Thread1 t1 = new Thread1();  7  t1.start();  8  t1.start();  9  t1.start(); 10  t1.start(); 11 // Thread1 t2 = new Thread1(); 12 // t2.start();
13  } 14 }
相关文章
相关标签/搜索