synchronized 无效
package Multithreading;
import java.util.Random;
import java.util.concurrent.locks.ReentrantLock;
public class RunningableDemo implements Runnable {
private Thread thread;
private String threadName;
static LifeValue lifeValue=new LifeValue();
private Random r=new Random();
RunningableDemo(String name){
threadName=name;
System.out.println("Creating "+threadName);
}
public synchronized int attack(String threadName){
if (lifeValue.getFlag()<=0) return 0;
int kind=r.nextInt(3)+1;
if(kind==1){
lifeValue.Attack1();
System.out.println(threadName+" attact 10");
System.out.println(threadName+" LifeValue="+lifeValue.getLifeValue());
}else if (kind==2){
lifeValue.Attack2();
System.out.println(threadName+" attact 20");
System.out.println(threadName+" LifeValue="+lifeValue.getLifeValue());
}else if (kind==3){
lifeValue.Attack3();
System.out.println(threadName+" attact 30");
System.out.println(threadName+" LifeValue="+lifeValue.getLifeValue());
}
if(lifeValue.getFlag()<=0){
System.out.println(threadName+" win");
}
return lifeValue.getFlag();
}
@Override
public void run() {
System.out.println("Running "+threadName);
try{
Thread.sleep(50);
for(int i=5;i>0;i--){
int flag=attack(threadName);
if(flag<=0) break;
Thread.sleep(60);
}
}catch (InterruptedException e){
System.out.println("Thread"+threadName+"interrupted.");
}
System.out.println("Thread"+threadName+"exiting.");
}
public void start(){
System.out.println("Starting "+threadName);
if (thread==null){
thread=new Thread(this,threadName);
thread.start();
}
}
}
import java.util.Random;
import java.util.concurrent.locks.ReentrantLock;
public class RunningableDemo implements Runnable {
private Thread thread;
private String threadName;
static LifeValue lifeValue=new LifeValue();
private Random r=new Random();
RunningableDemo(String name){
threadName=name;
System.out.println("Creating "+threadName);
}
public synchronized int attack(String threadName){
if (lifeValue.getFlag()<=0) return 0;
int kind=r.nextInt(3)+1;
if(kind==1){
lifeValue.Attack1();
System.out.println(threadName+" attact 10");
System.out.println(threadName+" LifeValue="+lifeValue.getLifeValue());
}else if (kind==2){
lifeValue.Attack2();
System.out.println(threadName+" attact 20");
System.out.println(threadName+" LifeValue="+lifeValue.getLifeValue());
}else if (kind==3){
lifeValue.Attack3();
System.out.println(threadName+" attact 30");
System.out.println(threadName+" LifeValue="+lifeValue.getLifeValue());
}
if(lifeValue.getFlag()<=0){
System.out.println(threadName+" win");
}
return lifeValue.getFlag();
}
@Override
public void run() {
System.out.println("Running "+threadName);
try{
Thread.sleep(50);
for(int i=5;i>0;i--){
int flag=attack(threadName);
if(flag<=0) break;
Thread.sleep(60);
}
}catch (InterruptedException e){
System.out.println("Thread"+threadName+"interrupted.");
}
System.out.println("Thread"+threadName+"exiting.");
}
public void start(){
System.out.println("Starting "+threadName);
if (thread==null){
thread=new Thread(this,threadName);
thread.start();
}
}
}
...全文
相关推荐
synchronized优化详解[可运行源码]
为了避免高竞争下过多的线程自旋导致资源浪费,自适应自旋锁技术应运而生,它根据对象锁的历史竞争状况,动态调整自旋等待的次数,减少无效的自旋操作。
Lock、Synchoronized和ReentrantLock的使用
Atomic 的缺点是只能同步一个值,一段代码中只能出现一个 Atomic 变量,多于一个同步无效。
2020面试题总结多线程篇.pdf
* volatile 可以保证可见性,当一个变量被 volatile 修饰后,表示着线程本地内存无效,当一个线程修改共享变量后他会自己被更新到主内存中,当其他线程读取共享变量时,他会直接从主内存中读取。
透彻理解Java中Synchronized(对象锁)和Static Synchronized(类锁)的区别
Java 中 Synchronized(对象锁)和 Static Synchronized(类锁)的区别Synchronized 和 Static Synchronized 是 Java 中两种同步机制
防止恶意刷新页面的java实现
这两种方法都可以有效地减少无效或恶意的请求,保护服务器资源,提高系统的稳定性和安全性。在实际开发中,可以根据应用场景和需求,选择适合的防刷新策略,或者结合使用,以达到更好的效果。


Java SE
