您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Java中如何实现Runnable接口?

2024/3/18 22:01:36发布28次查看
但是java runnable 接口并没有任何对线程的支持,我们还必须创建 thread 类的实例,这一点通过 thread 类的构造函数public thread(runnable target);来实现。下面是一个例子:
public class mythread implements runnable {   int count=1, number;   public mythread(int num) {   numnumber = num;   system.out.println(创建线程  + number);   }   public void run() {   while(true) {   system.out.println(线程  + number + :计数  + count);   if(++count== 6) return;   }   }   public static void main(string args[]) {   for(int i = 0; i < 5; i++)   new thread(new mythread(i+1)).start();   }   }
使用java runnable 接口来实现多线程使得我们能够在一个类中包容所有的代码,有利于封装下面让我们一起来研究一下多线程使用中的一些问题。
以上就是java中如何实现runnable接口?的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product