标签:https 实例 type 启动 run oid rate ima dia
public class MyThread extends Thread { private int count=0; @Override public void run() { System.out.println("线程已启动"); while(count<10000) { count++; } System.out.println("count:"+count); } }
public class TestMyThread { /* * 继承Thread类创建线程 * 1、继承Thread类 * 2、重写run方法 * 3、实例化线程对象 * 4、调用start方法启动线程 * * 继承Thread类会存在什么问题? * 线程类不能继承其他类 */ public static void main(String[] args) { //实例化线程对象 Thread myThread=new MyThread(); //启动线程 myThread.start(); /* * start方法的作用:该方法会使操作系统初始化一个新的线程,由这个新的线程来执行线程对象的run方法 */ } }
public class MyRunnable implements Runnable { private int count=0; @Override public void run() { count++; System.out.println("count:"+count); } }
public class TestMyRunnable { public static void main(String[] args) { // TODO Auto-generated method stub Runnable my=new MyRunnable(); System.out.println("thread启动了"); Thread thread=new Thread(my); System.out.println("thread2启动了"); Thread thread_2=new Thread(my); thread.start(); thread_2.start(); } }
标签:https 实例 type 启动 run oid rate ima dia
原文地址:https://www.cnblogs.com/schangxiang/p/11173860.html