黑马程序员技术交流社区
标题:
关于多线程实现Runnable接口问题
[打印本页]
作者:
四川男人
时间:
2014-4-3 22:37
标题:
关于多线程实现Runnable接口问题
JAVA实现多线程有两种方式:
一种是继承Thread类并重写run()方法;具体实现是在main()函数中创建Thread子类对象,并调用start()方法开启线程和run()方法。
另一种是实现Runnable接口,实现run()方法;至于具体实现我总是没搞懂。
各位前辈高人给点儿指教吧,通俗易懂一点儿就好!!!!!!!!!
作者:
lilin202011
时间:
2014-4-3 23:17
一个类如果需要具备多线程的能力,也可以通过实现java.lang.Runnable接口进行实现。按照Java语言的语法,一个类可以实现任意多个接口,所以该种实现方式在实际实现时的通用性要比前面介绍的方式好一些。
/**
* 测试类
*/
public class Test2 {
public static void main(String[] args) {
//创建对象
MyRunnable mr = new MyRunnable();
Thread t = new Thread(mr);
//启动
t.start();
try{
for(int i = 0;i < 10;i++){
Thread.sleep(1000);
System.out.println("main:" + i);
}
}catch(Exception e){}
}
}
/**
* 使用实现Runnable接口的方式实现多线程
*/
public class MyRunnable implements Runnable {
public void run() {
try{
for(int i = 0;i < 10;i++){
Thread.sleep(1000);
System.out.println("run:" + i);
}
}catch(Exception e){}
}
}
复制代码
该示例代码实现的功能和前面实现的功能相同。在使用该方式实现时,使需要实现多线程的类实现Runnable,实现该接口需要覆盖run方法,然后将需要以多线程方式执行的代码书写在run方法内部或在run方法内部进行调用。
在需要启动线程的地方,首先创建MyRunnable类型的对象,然后再以该对象为基础创建Thread类的对象,最后调用Thread对象的start方法即可启动线程。代码如下:
//创建对象
MyRunnable mr = new MyRunnable();
Thread t = new Thread(mr);
//启动
t.start();
在这种实现方式中,大部分和前面介绍的方式类似,启动的代码稍微麻烦一些。这种方式也是实现线程的一种主要方式。
作者:
muma
时间:
2014-4-3 23:30
class Ticket implements Runnable//extends Thread
{
private int tick = 100;
public void run()
{
while(true)
{
if(tick>0)
{
System.out.println(Thread.currentThread().getName()+"....sale : "+ tick--);
}
}
}
}
class TicketDemo
{
public static void main(String[] args)
{
Ticket t = new Ticket();
Thread t1 = new Thread(t);//创建了一个线程;
Thread t2 = new Thread(t);//创建了一个线程;
Thread t3 = new Thread(t);//创建了一个线程;
Thread t4 = new Thread(t);//创建了一个线程;
t1.start();
t2.start();
t3.start();
t4.start();
/*
Ticket t1 = new Ticket();
//Ticket t2 = new Ticket();
//Ticket t3 = new Ticket();
//Ticket t4 = new Ticket();
t1.start();
t1.start();
t1.start();
t1.start();
*/
}
}
复制代码
创建线程的第二种方式:实现Runable接口
步骤:
1,定义类实现Runnable接口
2,覆盖Runnable接口中的run方法。将线程要运行的代码存放在该run方法中。
3,通过Thread类建立线程对象。
4,将Runnable接口的子类对象作为实际参数传递给Thread类的构造函数。
为什么要将Runnable接口的子类对象传递给Thread的构造函数。
因为,自定义的run方法所属的对象是Runnable接口的子类对象。
所以要让线程去执行指定对象的run方法。就必须明确该run方法所属对象。
5,调用Thread类的start方法开启线程并调用Runnable接口子类的run方法。
实现方式和继承方式有什么区别呢?
实现方式好处:避免了单继承的局限性。
在定义线程时,建立使用实现方式。
两种方式区别:
继承Thread:线程代码存放Thread子类run方法中。
实现Runnable,线程代码存在接口的子类的run方法。
作者:
._抬眉﹏
时间:
2014-4-3 23:40
还有一种方法就是写一个匿名内部类,匿名内部类是一种特殊的局部内部类,因为有时候为了方便就不用再外面写它的类了,直接在内部写一个匿名的thread的子类,具体实现代码看下面:package 多线程;
//匿名内部类的方式创建线程
public class ThreadDemo1 {
public static void main(String arr[]){
Thread thread=new Thread(){
public void run() {
while (true) {
System.err.println("1"+Thread.currentThread().getId()+Thread.currentThread().getName());
}
}
};
Thread thread2=new Thread(new Runnable() {
@Override
public void run() {
while (true) {
System.err.println("2"+Thread.currentThread().getId()+Thread.currentThread().getName());
}
}
});
thread.start();
thread2.start();
}
}
但是有一个缺点就是匿名内部类找不到类的对象名字。只是很方便使用这个对象,在这里我觉得只使用一次的话用匿名内部类比较好一点。
作者:
杨殿生
时间:
2014-4-4 00:09
class Ticket implements Runnable//extends Thread
{
private int tick=100;
public void run()
{
while(true)
{
if(tick>0)
System.out.println(Thread.currentThread().getName()+" "+"sale "+tick--);
}
}
}
class TicketDemo
{
public static void main(String[] args)
{
Ticket t=new Ticket();
Thread t1=new Thread(t);
Thread t2=new Thread(t);
Thread t3=new Thread(t);
Thread t4=new Thread(t);
t1.start();
t2.start();
t3.start();
t4.start();
//Ticket t1=new Ticket();
//Ticket t2=new Ticket();
//Ticket t3=new Ticket();
//Ticket t4=new Ticket();
//t1.start();
//t1.start();
///t1.start();
//t1.start();
}
}
复制代码
1,定义类实现Runnable接口
2,覆盖Runnable接口中的run方法
3,通过Thread类建立线程对象
4,将Runnable接口的子类对象作为实际参数传递给Thread类的构造函数
5,调用Thread类的start方法开启线程并调用Runnable接口子类的run方法
作者:
yuon11937
时间:
2014-4-4 10:04
本帖最后由 yuon11937 于 2014-4-4 12:50 编辑
第一种方法是创建一个Thread类的子类定义一个线程,覆盖Thread类的run方法,创建Thread子类的对象,调用start方法启动线程。这种方法已经不再推荐使用了。举个简单例子你就会明白了:
class TestThread extends Thread {
public void run() {
for(int i=0;i<20;i++) {
System.out.println("自定义线程" + Thread.currentThread().getName() + i);
}
}
}
public class MyThread
{
public static void main(String[] args) {
System.out.println("主线程" + Thread.currentThread().getName());
TestThread tt = new TestThread();
tt.start();
}
}
第二种方式:实现Runnable接口,覆盖接口中的run方法,创建Thread类的对象,将实现Runnable接口的子类对象,作为参数传递给Thread类的对象,最后调用Thread的start方法启动线程。实例代码如下:
class TestRunnable implements Runnable {
public void run() {
for(int i=0;i<20;i++) {
System.out.println("自定义线程" + Thread.currentThread().getName() + i);
}
}
}
public class MyThread
{
public static void main(String[] args) {
System.out.println("主线程" + Thread.currentThread().getName());
TestRunnable tt = new TestRunnable ();
Thread t1 = new Thread(tt);
Thread t2 = new Thread(tt);
t1.start();
t2.start();
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2