package com.test;
public class ThreadSafeTest extends Thread implements Runnable {
private static int num = 1;
public ThreadSafeTest(String name) {
setName(name);
}
public void run() {
sell(getName());
}
private synchronized void sell(String name){
if (num > 0) {
System. out.println(name + ": 检测票数大于0" );
System. out.println(name + ": \t正在收款(大约5秒完成)。。。" );
try {
Thread. sleep(5000);
System. out.println(name + ": \t打印票据,售票完成" );
num--;
printNumInfo();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
System. out.println(name+": 没有票了,停止售票" );
}
}
private static void printNumInfo() {
System. out.println("系统:当前票数:" + num);
if (num < 0) {
System. out.println("警告:票数低于0,出现负数" );
}
}
public static void main(String args[]) {
try {
new ThreadSafeTest("售票员李XX" ).start();
Thread. sleep(2000);
new ThreadSafeTest("售票员王X" ).start();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
售票员李XX: 检测票数大于0 售票员李XX: 正在收款(大约5秒完成)。。。 售票员王X: 检测票数大于0 售票员王X: 正在收款(大约5秒完成)。。。 售票员李XX: 打印票据,售票完成 系统:当前票数:0 售票员王X: 打印票据,售票完成 系统:当前票数:-1 警告:票数低于0,出现负数
售票员李XX: 检测票数大于0 售票员李XX: 正在收款(大约5秒完成)。。。 售票员王X: 检测票数大于0 售票员王X: 正在收款(大约5秒完成)。。。 售票员李XX: 打印票据,售票完成 系统:当前票数:0 售票员王X: 打印票据,售票完成 系统:当前票数:0
package com.test;
public class ThreadSafeTest extends Thread implements Runnable {
private static int num = 1;
public ThreadSafeTest(String name) {
setName(name);
}
public void run() {
sell(getName());
}
private synchronized static void sell(String name){
if (num > 0) {
System. out.println(name + ": 检测票数大于0" );
System. out.println(name + ": \t正在收款(大约5秒完成)。。。" );
try {
Thread. sleep(5000);
System. out.println(name + ": \t打印票据,售票完成" );
num--;
printNumInfo();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
System. out.println(name+": 没有票了,停止售票" );
}
}
private static void printNumInfo() {
System. out.println("系统:当前票数:" + num);
if (num < 0) {
System. out.println("警告:票数低于0,出现负数" );
}
}
public static void main(String args[]) {
try {
new ThreadSafeTest("售票员李XX" ).start();
Thread. sleep(2000);
new ThreadSafeTest("售票员王X" ).start();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
售票员李XX: 检测票数大于0 售票员李XX: 正在收款(大约5秒完成)。。。 售票员李XX: 打印票据,售票完成 系统:当前票数:0 售票员王X: 没有票了,停止售票
class Parent {
public synchronized void method() { }
}
class Child extends Parent {
public synchronized void method () { }
}
class Parent {
public synchronized void method() { }
}
class Child extends Parent {
public void method() { super.method(); }
}
public synchronized int n = 0; public static synchronized int n = 0;
public class MyThread1 extends Thread {
public String methodName ;
public static void method(String s) {
System. out .println(s);
while (true );
}
public synchronized void method1() {
method( "非静态的method1方法" );
}
public synchronized void method2() {
method( "非静态的method2方法" );
}
public static synchronized void method3() {
method( "静态的method3方法" );
}
public static synchronized void method4() {
method( "静态的method4方法" );
}
public void run() {
try {
getClass().getMethod( methodName ).invoke( this);
}
catch (Exception e) {
}
}
public static void main(String[] args) throws Exception {
MyThread1 myThread1 = new MyThread1();
for (int i = 1; i <= 4; i++) {
myThread1. methodName = "method" + String.valueOf (i);
new Thread(myThread1).start();
sleep(100);
}
}
}
非静态的method1方法 静态的method3方法
package net.mindview.util;
import javax.swing.JFrame;
public class WaitAndNotify {
public static void main(String[] args) {
System. out.println("Hello World!" );
WaitAndNotifyJFrame frame = new WaitAndNotifyJFrame();
frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE);
// frame.show();
frame.setVisible( true);
}
}
@SuppressWarnings("serial" )
class WaitAndNotifyJFrame extends JFrame {
private WaitAndNotifyThread t ;
public WaitAndNotifyJFrame() {
setSize(300, 100);
setLocation(250, 250);
JPanel panel = new JPanel();
JButton start = new JButton(new AbstractAction("Start") {
public void actionPerformed(ActionEvent event) {
if (t == null) {
t = new WaitAndNotifyThread(WaitAndNotifyJFrame.this);
t.start();
} else if (t .isWait ) {
t. isWait = false ;
t.n();
// t.notify();
}
}
});
panel.add(start);
JButton pause = new JButton(new AbstractAction("Pause") {
public void actionPerformed(ActionEvent e) {
if (t != null) {
t. isWait = true ;
}
}
});
panel.add(pause);
JButton end = new JButton(new AbstractAction("End") {
public void actionPerformed(ActionEvent e) {
if (t != null) {
t.interrupt();
t = null;
}
}
});
panel.add(end);
getContentPane().add(panel);
}
}
@SuppressWarnings("unused" )
class WaitAndNotifyThread extends Thread {
public boolean isWait ;
private WaitAndNotifyJFrame control ;
private int count ;
public WaitAndNotifyThread(WaitAndNotifyJFrame f) {
control = f;
isWait = false ;
count = 0;
}
public void run() {
try {
while (true ) {
synchronized (this ) {
System. out.println("Count:" + count++);
sleep(100);
if (isWait )
wait();
}
}
} catch (Exception e) {
}
}
public void n() {
synchronized (this ) {
notify();
}
}
}
public synchronized void n() {
notify();
}
public void n() {
synchronized (this ) {
notify();
}
}
public class SynchronizedStatic implements Runnable {
private static boolean flag = true;
//类对象同步方法一:
// 注意static修饰的同步方法,监视器:SynchronizedStatic.class
private static synchronized void testSyncMethod() {
for (int i = 0; i < 100; i++) {
try {
Thread. sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System. out.println("testSyncMethod:" + i);
}
}
//类对象同步方法二:
private void testSyncBlock() {
// 显示使用获取class做为监视器.它与static synchronized method隐式获取class监视器一样.
synchronized (SynchronizedStatic. class) {
for (int i = 0; i < 100; i++) {
try {
Thread. sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System. out.println("testSyncBlock:" + i);
}
}
}
public void run() {
// flag是static的变量.所以,不同的线程会执行不同的方法,只有这样才能看到不同的锁定效果.
if (flag ) {
flag = false ;
testSyncMethod();
} else {
flag = true ;
testSyncBlock();
}
}
public static void main(String[] args) {
ExecutorService exec = Executors. newFixedThreadPool(2);
SynchronizedStatic rt = new SynchronizedStatic();
SynchronizedStatic rt1 = new SynchronizedStatic();
exec.execute(rt);
exec.execute(rt1);
exec.shutdown();
}
}
testSyncMethod:0 testSyncMethod:1 ... ... testSyncMethod:99 testSyncBlock:0 ... ... testSyncBlock:99
testSyncBlock:0 testSyncMethod:0 testSyncBlock:1 testSyncMethod:1 ... ... testSyncMethod:99 testSyncBlock:99
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有