在千锋学习的第二十九天
自己加油!
package com.lichenchen.t3;
public class TestDemo1 {
public static void main(String[] args) {
Thread t1 =new MyThread7("aaa");
Thread t2 =new MyThread7("bbb");
t1.start();
t2.start();
}
}
class MyThread7 extends Thread{
private String data;
public MyThread7(String data){
this.data =data;
}
public void run(){
synchronized(System.out){
for(int i =0;i<100;i++){
System.out.println(data);
}
}
}
}
C
package com.lichenchen.t3;
public class TestMyThread {
public static void main(String[] args) {
Runnable t1 =new MyThread1();
Thread t3 =new Thread(t1);
Thread t2 =new MyThread2();
t3.start();
t2.start();
}
}
class MyThread1 implements Runnable{
public void run(){
for(int i =0;i<100;i++){
try {
Thread.sleep((int)(Math.random()*1000));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("hello");
}
}
}
class MyThread2 extends Thread {
public void run(){
for(int i= 0;i<100;i++){
try {
Thread.sleep((int)(Math.random()*1000));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("world");
}
}
}