java线程使用示例——最简单的线程

java线程使用示例——最简单的线程

线程使用示例一:

public class ThreadTest {

	public static void main(String[] args) {
		//线程使用示例一:
		new Thread() {
			public void run() {
				while (true) {
					try {
						System.out.println("线程输出");
						
						//休眠两秒
						Thread.sleep(2 * 1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			};
		}.start();
	}

}
线程使用示例二:
//线程使用示例二:
        Thread t1 = new Thread(new Runnable(){
            public void run(){
                System.out.println("Mythread 线程t1");
                while(true){
                	Singleton3 single3 = Singleton3.getInstance(null);
                    System.out.println("线程t1 >> " + single3.toString());
                	try {
        				Thread.sleep(2000);
        			} catch (InterruptedException e) {
        				// TODO Auto-generated catch block
        				e.printStackTrace();
        			}
                }
                
            }
        });
        t1.start();


java线程使用示例——最简单的线程 线程使用示例一: public class ThreadTest { public static void main(String[] args) { //线程使用示例一: new Thread() { public void run() { while (true) { try { System.out.println("线程输出"); //休眠两秒 Thread.sleep(2 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } } }; }.start(); } } 线程使用示例二: //线程使用示例二: Thread t1 = new Thread(new Runnable(){ public void run(){ System.out.println("Mythread 线程t1"); while(true){ Singleton3 single3 = Singleton3.getInstance(null); System.out.println("线程t1 >> " + single3.toString()); try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }); t1.start();
经验分享 程序员 微信小程序 职场和发展