课堂代码
package kt;
//继承Thread类
public class L1 extends Thread{
int count = 0;
public L1(String name){
super(name);
}
public void run(){
for(int i = 0;i < 5;i++){
count++;
System.out.println(count + ":" + this.getName());
}
}
public static void main(String[] args){
L1 p = new L1("线程1");
int count = 0;
p.start();
for(int i = 0;i < 5 ; i++){
count ++;
System.out.println(count + ":main");
}
}
}
package kt;
//继承类
public class L2 extends Thread{
int count = 0;
static int c = 0;
public L2(String name){
super(name);
}
public void run(){
for(int i = 0;i < 5;i++){
c++;
System.out.println(
c + ":"
+ Thread.currentThread() + ":"
+ this.getName());
}
}
public static void main(String[] args){
L2 p1 = new L2("线程1");
L2 p2 = new L2("线程2");
p1.start();
p2.start();
}
}
package kt;
//接口类
public class L3 implements Runnable{
int count = 1;
int number;
public L3(int i){
number = i;
System.out.println("创建线程"+number);
}
public void run(){
while(true){
System.out.println("线程"+number+"计数"+count);
if(++count == 3){
return;
}
}
}
public static void main(String args[]){
for(int i = 0;i<3;i++){
Thread p = new Thread(new L3(i+1));
p.start();
}
}
}
package kt;
/*
* Runnable接口类
* */
public class L4 implements Runnable{
int n;
public void run(){
for(int i = 1;i < n;i++){
System.out.println(i + " ");
}
}
public static void main(String args[]){
L4 my = new L4();
my.n = 10;
Thread p1 = new Thread(my);
p1.start();
}
}