您的位置:首页 > 编程语言 > Java开发

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

2015-01-08 16:36 323 查看
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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: