62,616
社区成员
发帖
与我相关
我的任务
分享
synchronized (this) {
...
}
import java.util.Date;
class Knowing{
public static Boolean i = true;
public void start(){
test t1=new test("t1");
test t2=new test("t2");
test t3=new test("t3");
timer ti=new timer();
t1.start();
t2.start();
t3.start();
ti.start();
}
public static void main(String[] args){
Knowing k=new Knowing();
k.start();
}
class test extends Thread{
String name="";
public test(String name){
this.name=name;
}
public void run(){
while(true){
synchronized (i){
if(i){
System.out.println(name+":Hello!"+new Date(System.currentTimeMillis()).toLocaleString());
i=false;
}
}
}
}
}
class timer extends Thread{
int count=0;
public synchronized void run(){
while(true){
try {
Thread.sleep(1000);
count++;
if(count==2){
i = true;
count=0;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
synchronized(synchronizedBool){
if(synchronizedBool){
//Sing1
synchronizedBool = false;
..//执行逻辑
}
}
public class BaseAction extends DispatchAction {
private SpecialOption option = new SpecialOption();
public ActionForward update(ActionMap map, ActionForm form, ServletHttpRequest request, ServletHttpResponse response){
}
public ActionForward add(ActionMap map, ActionForm form, ServletHttpRequest request, ServletHttpResponse response){
synchronized (option) {// 同步option对象,而不是整个Action
option.****方法
}
}
public ActionForward delete(ActionMap map, ActionForm form, ServletHttpRequest request, ServletHttpResponse response){
}
}
// 特殊功能类
public class SpecialOption() {
}
void fun()
{
if(/*currentTime>=executeTime*/ && executedFlagBool==false)
{
//insert your operation here
executedFlagBool=true;
}
else if(/*currentTime>=resetTime*/)
executedFlagBool=false;
return;
}
if(synchronizedBool){
synchronizedBool = false;
..//执行逻辑
}
class myThread extends Thread{
static boolean synchronizedBool= true;
public void run(){
//判断时间点并设施synchronizedBool值
}
}
synchronized (this) {
if(synchronizedBool){//synchronizedBool 为你所说的判断条件
synchronizedBool = false;
if(nowIsExcuteTime){//判断是否到了执行时间
...//执行逻辑
}
}
synchronizedBool = true;
}