public class Ticket implements Runnable{
private int num;//票数量
private boolean flag=true;//若为false则售票停止
public Ticket(int num){
this.num=num;
}
@Override
public void run() {
while(flag){
ticket();
}
}
private void ticket(){
if(num<=0){
flag=false;
return;
}
try {
Thread.sleep(20);//模拟延时操作
} catch (InterruptedException e) {
e.printStackTrace();
}
//输出当前窗口号以及出票序列号
System.out.println(Thread.currentThread().getName()+"售出票序列号:"+num--);
}
}
public class MainTest {
public static void main(String[] args) {
Ticketticket = new Ticket(5);
Threadwindow01 = new Thread(ticket, "窗口01");
Threadwindow02 = new Thread(ticket, "窗口02");
Threadwindow03 = new Thread(ticket, "窗口03");
window01.start();
window02.start();
window03.start();
}
}
窗口02售出票序列号:5 窗口03售出票序列号:4 窗口01售出票序列号:5 窗口02售出票序列号:3 窗口01售出票序列号:2 窗口03售出票序列号:2 窗口02售出票序列号:1 窗口03售出票序列号:0 窗口01售出票序列号:-1
//方式一
private synchronized void ticket(){
if(num<=0){
flag=false;
return;
}
try {
Thread.sleep(20);//模拟延时操作
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"售出票序列号:"+num--);
}
//方式二
private void ticket(){
synchronized (this) {
if (num <= 0) {
flag = false;
return;
}
try {
Thread.sleep(20);//模拟延时操作
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "售出票序列号:" + num--);
}
}
窗口01售出票序列号:5 窗口03售出票序列号:4 窗口03售出票序列号:3 窗口02售出票序列号:2 窗口02售出票序列号:1
public class Singleton {
private static Singletoninstance;
private Singleton() {
}
public static SingletongetInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
com.sunny.singleton.Singleton@15c330aa com.sunny.singleton.Singleton@15c330aa com.sunny.singleton.Singleton@41aff40f
public static SingletongetInstance() {
if (instance == null) {
synchronized (Singleton.class) {
instance = new Singleton();
}
}
return instance;
}
public static SingletongetInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if(instance==null){
instance = new Singleton();
}
}
}
return instance;
}
public class Parent {
public synchronized void test() {
for (int i = 0; i < 5; i++) {
System.out.println("Parent " + Thread.currentThread().getName() + ":" + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Child extends Parent {
@Override
public void test() {
for (int i = 0; i < 5; i++) {
System.out.println("Child " + Thread.currentThread().getName() + ":" + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
final Child c = new Child();
new Thread() {
public void run() {
c.test();
};
}.start();
new Thread() {
public void run() {
c.test();
};
}.start();
Parent Thread-0:0 Child Thread-0:0 Parent Thread-0:1 Child Thread-1:0 Parent Thread-0:2 Child Thread-0:1 Parent Thread-0:3 Child Thread-1:1 Parent Thread-0:4 Child Thread-0:2 Parent Thread-1:0 Child Thread-1:2 Parent Thread-1:1 Child Thread-0:3 Parent Thread-1:2 Child Thread-1:3 Parent Thread-1:3 Child Thread-0:4 Parent Thread-1:4 Child Thread-1:4
public class Thread01 extends Thread{
private Object resource01;
private Object resource02;
public Thread01(Object resource01, Object resource02) {
this.resource01 = resource01;
this.resource02 = resource02;
}
@Override
public void run() {
synchronized(resource01){
System.out.println("Thread01 locked resource01");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (resource02) {
System.out.println("Thread01 locked resource02");
}
}
}
}
public class Thread02 extends Thread{
private Object resource01;
private Object resource02;
public Thread02(Object resource01, Object resource02) {
this.resource01 = resource01;
this.resource02 = resource02;
}
@Override
public void run() {
synchronized(resource02){
System.out.println("Thread02 locked resource02");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (resource01) {
System.out.println("Thread02 locked resource01");
}
}
}
}
public class MainTest {
public static void main(String[] args) {
final Object resource01="resource01";
final Object resource02="resource02";
Thread01thread01=new Thread01(resource01, resource02);
Thread02thread02=new Thread02(resource01, resource02);
thread01.start();
thread02.start();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有