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

多线程

时间:2016-04-18 22:39:02      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:

多线程:

  1. 进程(process):通常指一个正在运行的的程序;每个进程都有自己的独立的一块的内存空间,每个进程的内部数据和状态都是完全独立的
  2. 线程:一个程序在同一时间运行多个任务,每个任务成为一个线程
  3. 多线程程序:能够在一个程序内运行多线程的程序
  4. 线程和进程的区别:线程存在于进程中;每个进程都需要操作系统为其分配独立的内存空间,而同一进程中的所有线程都在同一内存中工作,这些线程可以共享同一块内存和系统资源、

线程的创建:

1.通过继承Thread类的类,重写run()方法:


工具:MyEclipse

 1 /**
 2  * 
 3  * @author Administrator
 4  *继承Thread类
 5  */
 6 public class Thread1 extends Thread{
 7 
 8     @Override
 9     public void run() {
10         // TODO Auto-generated method stub
11         for(int i=0;i<10;i++){//方便举例:run()方法这里只打印10句Hello world和名字
12             System.out.println("Hello world!"+getName());
13         }
14     }
15 
16 }

 

 

 

 1 public class Test {
 2 
 3     /**
 4      * @param args
 5      */
 6     public static void main(String[] args) {
 7         // TODO Auto-generated method stub
 8         Thread1 a=new Thread1();
 9         a.setName("this is my name!");//通过Thread类的setName(),getName()方法的设置和得到名字,具体查看API帮助文档
10         a.start();
11     }
12 
13 }
运行的结果:

Hello world!this is my name!
Hello world!this is my name!
Hello world!this is my name!
Hello world!this is my name!
Hello world!this is my name!
Hello world!this is my name!
Hello world!this is my name!
Hello world!this is my name!
Hello world!this is my name!
Hello world!this is my name!

2.编写一个实现Runnable接口的类:


 

 1 /**
 2  * 
 3  * @author Administrator
 4  *实现Runnable接口
 5  */
 6 public class Runnable1 implements Runnable{
 7 
 8     @Override
 9     public void run() {
10         // TODO Auto-generated method stub
11         for(int i=0;i<10;i++){//方便举例:run()方法这里只打印10句Hello world和名字
12             System.out.println("Hello world!"+Thread.currentThread().getName());//Runnable接口中没有getName()方法,只有用Thread类的currentThread().getName()方法得到名字
13         }
14     }
15 }
 1 public class Test {
 2 
 3     /**
 4      * @param args
 5      */
 6     public static void main(String[] args) {
 7         // TODO Auto-generated method stub
 8         Runnable1 r=new Runnable1();
 9         Thread thread=new Thread(r);
10         thread.setName("This is a name!");//Runnable接口中没有setName()方法,使用Thread类
11         thread.start();//Runnable接口中没有start()方法,使用Thread类
12     }
13 
14 }
1 public static void main(String[] args) {
2         // TODO Auto-generated method stub
3         new Thread(new Runnable1(),"This is a name").start();//这里用了匿名类的一种形式,结果和之前一样
4         
5         
6         
7     }
8 
9 }
 1 public class Test {
 2 
 3     /**
 4      * @param args
 5      */
 6     public static void main(String[] args) {
 7         // TODO Auto-generated method stub
 8         
 9         new Thread(new Runnable(){
10             public void run(){
11                 for(int i=0;i<10;i++){//方便举例:run()方法这里只打印10句Hello world和名字
12                         System.out.println("Hello world!"+Thread.currentThread().getName());
13                 }
14             }
15         },"This is a name!").start();//这里用了匿名类的一种形式,结果和之前的一样
16         
17     }
18 
19 }
运行的结果:

Hello world!This is a name!
Hello world!This is a name!
Hello world!This is a name!
Hello world!This is a name!
Hello world!This is a name!
Hello world!This is a name!
Hello world!This is a name!
Hello world!This is a name!
Hello world!This is a name!
Hello world!This is a name!

 实现Runnable的方式相对继承Thread类的方式优势:1.可以避免java单继承的局限;2.使用Runnable接口可以将虚拟Cpu(Thread类)与线程要完成的任务有效分离,更好的体现面向对象思想

 

多线程

标签:

原文地址:http://www.cnblogs.com/vencent-2016/p/5405973.html

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