import java.util.Arrays;
public class maopao extends Thread{
public static int a=9;
String k=new String("");
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("进入线程");
super.run();
synchronized (k){
try {
this.sleep(6000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i=0;i<6;i++){
System.out.println(i);
}
}
}
public static void main(String[] args) {
// 一种方法
maopao m=new maopao();
Thread t1=new Thread(m);
Thread t2=new Thread(m);
Thread t3=new Thread(m);
t1.start();
t2.start();
t3.start();
}
多窗口售票系统 线程
- package net.okren.java;
- class SaleTicket implements Runnable{
- private int tickets = 100;
- public void run(){
- while(tickets > 0){
- sale();
- }
- }
- private synchronized void sale(){
- if(tickets > 0){
- System.out.println(Thread.currentThread().getName() + "卖出 第 "+ (100 - tickets + 1)+"张票");
- tickets--;
- try{
- Thread.sleep(500);
- }catch(InterruptedException e){
- e.printStackTrace();
- }
- }
- }
- }
- public class JavaTest {
- public static void main(String[] args){
- SaleTicket st = new SaleTicket();
- Thread t1 = new Thread(st, "一号窗口");
- Thread t2 = new Thread(st, "二号窗口");
- Thread t3 = new Thread(st, "三号窗口");
- Thread t4 = new Thread(st,"四号窗口 ");
- t1.start();
- t2.start();
- t3.start();
- t4.start();
- }
- }