java160107ThreadTest

来源:互联网 发布:社交网络上的跟风行为 编辑:程序博客网 时间:2024/05/17 00:01
/**
 * 创建两个线程,和主线程交替运行
 * 线程调用两点class  className extends Thread 
 * 在className中覆盖run(),run中写自己要交给线程要做的demo
 * 在调用类中由className的实例调用  start()即可
 * 
 */
package java160107;


/**
 * @author LiZheng
 *
 */
public class ThreadTest {


/**
* @param args
*/
public static void main(String[] args) {
// Test test1 = new Test("one");
// Test test2 = new Test("two");
// Test test1 = new Test();
// Test test2 = new Test();
Test test1 = new Test("one++");
Test test2 = new Test("two++");
test1.start();
test2.start();
for (int i = 0; i < 60; i++) {
System.out.println("main-----" + i);
}
}


}


class Test extends Thread {
// private String name;


// public Test(String name) {
// this.name=name;
// }
public Test(String name) {
// this.name=name;
super(name);
}



@Override
public void run() {
super.run();
for (int x = 0; x < 60; x++) {
// System.out.println(name+"---test run---" + x);


// System.out.println(this.getName() + "---test run---" + x);


System.out.println(Thread.currentThread().getName() + "---test run---" + x);
}
}


}
0 0