package edu.xcdq.demo01;
public class Demo01 {
public static void main(String[] args) {
/*MyThread myThrad1 = new MyThread("自定义的线程名007");
myThrad1.start(); //线程调用 //myThread1.run(); //普通调用*/
MyThread t1=new MyThread("线程1");
MyThread t2=new MyThread("线程2");
MyThread t3=new MyThread("线程3");
MyThread t4=new MyThread("线程4");
MyThread t5=new MyThread("线程5");
MyThread t6=new MyThread("线程6");
MyThread t7=new MyThread("线程7");
MyThread t8=new MyThread("线程8");
MyThread t9=new MyThread("线程9");
MyThread t10=new MyThread("线程10");
t1.setPriority(1);
t2.setPriority(2);
t3.setPriority(3);
t4.setPriority(4);
t5.setPriority(5);
t6.setPriority(6);
t7.setPriority(7);
t8.setPriority(8);
t9.setPriority(9);
t10.setPriority(10);
t1.start()
;t2.start();
t3.start();
t4.start();
t5.start();
6.start();
t7.start();t
8.start();
t9.start();
t10.start();
}
}
/*
1 extends Thread
2 覆写run方法 3 调用start方法启动多线程 */
package edu.xcdq.demo01;
public class MyThreadextends Thread{
public MyThread(String name){
super(name);
}
public void run (){
System.out.println("自己的创建的线程");
System.out.println("线程名字:"+Thread.currentThread().getName());
}
}
package edu.xcdq.demo02;
public class Demo02 {
public static void main(String[] args) {
MyThread myThread=new MyThread();
Thread thread=new Thread(myThread);//把自定义类对象加入到系统线程
thread.start();
}
}
package edu.xcdq.demo02;
public class Demo02 {
public static void main(String[] args) {
MyThread myThread=new MyThread();
Thread thread=new Thread(myThread);//把自定义类对象加入到系统线程
thread.start();
}
1.package edu.xcdq.demo01;
public class TickerContent {
public static int count=50;
}
2.package edu.xcdq.demo01;
public class TestDemo01 {
public static void main(String[] args) {
Runnable mythread=new Runnable() {
@Override
public void run() {
synchronized (this) {
System.out.println(Thread.currentThread().getName()
+"卖出一张票,还剩:" + (--TickerContent.count)+"张"
);
}
}
};
new Thread(mythread,"线程1" ).start();
new Thread(mythread,"线程2" ).start();
new Thread(mythread,"线程3" ).start();
new Thread(mythread,"线程4" ).start();
}
}