上后谈爱情

导航

 

Java中最重要理念:线程是一个程序里面中不同的执行路径

 

2.例子:从下面看出只有一条路径,main()只是主进程,进程执行:进程中主线程开始进行;进程VS线程概念

线程是进程较小的划分单位,I.程序执行过程中至少有一个进程,一个进程中至少有一个线程,进程出现多并发的线程。

II.线程:进程中main方法,线程是进程执行的不同路径,所以线程改变只会代表CPU的执行过程的改边

III.一个CPU在某个时刻只会执行一个线程,多线程:CPU是双核,

 

3.

 1 package TestThread;
 2 
 3 public class TestThread1 {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         Runner1 r=new Runner1();
 8         //采用定义线程类实现Runnable接口,传入Thread(target),在自己定义
 9         //类中在重写run方法
10         Thread t= new Thread(r);
11         t.start();//开始一个线程,在main()会自动往下进行
12         
13         for (int i=1;i<100;i++)
14         {
15             
16             System.out.println("start Thread........."+ i);
17         }
18         
19     }
20 
21 }
22 //run 方法再Thread类的一个方法,使用接口概念,implements方法
23     class Runner1 implements Runnable{
24         public  void run(){
25             for (int i=0;i<100;i++)
26             {
27                 
28                 System.out.println("Runner1: "+ i);
29             }
30             
31         }
32     }

在这里面输出结果是先输出Main()方法中值,在输出Runnable中的值

使用继承的方法,一般最好使用接口的方式做线程

 1 package TestThread;
 2 
 3 public class TestThread1 {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         Runner1 r=new Runner1();
 8         //采用定义线程类实现Runnable接口,传入Thread(target),在自己定义
 9         //类中在重写run方法
10     //    Thread t= new Thread(r);
11     //    t.start();//开始一个线程,在main()会自动往下进行
12         r.start();
13         for (int i=1;i<100;i++)
14         {
15             
16             System.out.println("start Thread........."+ i);
17         }
18         
19     }
20 
21 }
22 //run 方法再Thread类的一个方法,使用接口概念,implements方法
23     class Runner1 extends Thread{
24         public  void run(){
25             for (int i=0;i<100;i++)
26             {
27                 
28                 System.out.println("Runner1: "+ i);
29             }
30             
31         }
32     }

4.线程状态转换:

 

 1 package TestThread;
 2 import java.lang.Thread;
 3 
 4 public class ThreadJoin {
 5     public static void main(String[] args){
 6         MyThread2 t1=new MyThread2("abcd");
 7         //开启线程
 8         t1.start();
 9         try{
10                 t1.join();//原本进程在t1.start()过程会有一个分支进程,通过t1.join方法,将分支进程加入main主进程中
11                 //腾出一个线程,本来两个方法会自动一会输出,变成一个进程
12                 
13             }
14         catch(InterruptedException e){}
15         for (int i=1;i<=10;i++)
16         {
17             System.out.println("i am main thread");
18         }
19         
20         
21     }
22 
23 }
24 
25 class MyThread2 extends Thread{
26     MyThread2    (String s) 
27     {
28         super(s);
29     }
30     public void run()
31     {for(int i=1;i<=10;i++)
32     {
33            System.out.println("i ous "+getName());
34            try
35            { 
36                sleep(1000) ;
37            }
38            catch(InterruptedException e)
39            {
40                return;
41            }
42     }
43     }
44     
45     
46 }

6.线程优先级别

Thread t1=new Thread(new T1())

t1.setProiority(Thread.NORM_PRIORITY=3)

 

自动停止线程方法:

 

 1 package TestThread;
 2 //在这里测试如何自动的关闭一个小的线程
 3 public class TestShutDown implements Runnable {
 4     boolean flag=true;
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7             TestShutDown t1=new TestShutDown();
 8             Thread tt=new Thread(t1);
 9             tt.start();
10             for (int i=1;i<100;i++)
11             {
12                 if(i%10==0)
13                 {
14                     System.out.println("Main 中i= "+ i);
15                 }
16                 
17             }
18             //关闭采用哪个tt还是t1(经过测试,还是t1.因为tt是Thread的对象,传替参数是t1还是采用t1),
19             t1.shutdown();
20     }
21    public void run()
22     { int i=0;
23        while(flag)
24        {
25            System.out.println("分支程序中i= "+ (++i));
26         /*   if(i==30)
27            {
28                break;
29            }*/
30                
31        }
32     }
33    public void shutdown()
34    {
35        flag=false;
36    }
37 }

 

posted on 2016-08-14 20:39  上后谈爱情  阅读(209)  评论(0编辑  收藏  举报