首页 > 代码库 > Java 多线程的实现方法
Java 多线程的实现方法
package com.jckb; /**多线程实现的两种方法 * * @author gx * */ public class Test2 { public static void main(String[] args) { Mythread m = new Mythread(); m.start();// 不能直接调用run方法 // m.run();//是方法调用,不是线程的启动 Thread t = new Thread(new Mythread2()); t.start(); } } // 线程停止的方法是run方法返回 // flag=false退出 // 线程停止后不能再次启动 class Mythread extends Thread { @Override public void run() { boolean flag = true; int i = 1; while (flag) { System.out.println("i=" + i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } i++; if (i == 5) { flag = false; // return; } } } } class Mythread2 implements Runnable { @Override public void run() { for (int i = 0; i < 10; i++) { System.out.println("i=" + i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
Java 多线程的实现方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。