Android相当于dispatch_once(Objective-C)

时间:2015-11-06 07:48:49

标签: java android

我想知道Android中的dict_of_strings = { 1:"PoopyPants", #my go-to programming word... i donno what to tell you... 2:"SleighBells", #i asked my girlfriend or the next two 3:"CookieMonster", 4:"bomb", #i have noidea how this one came to me 5:"Blellow", #when you mix blue and yellow, according to reece from Malcom in the Middle 6:"RandomWordOne", #because, random 7:"Flaccid", #theres a pattern to this word 8:"Solid", #maybe this is the pattern 9:"VaultOneHundredEleven", #FALLOUT4 HYPETRAIN 10:"SirDoctorMcFlippertonTheTwentySecondOfTheHouseOfLordProfessorSteveWilkerton" #this was a fun, convoluted phrase to come up with } def choice_is_rand(dict_of_strings): rand_word = str(dict_of_strings[random.randrange(0,10)]) #did i get this range correctly? i've always had trouble. I thought I had it, but i guess not... rand_backward = rand_word[::-1] print(rand_word) print(rand_backward.lower()) 相当于只发送一次Traceback (most recent call last): File "I:\User\My Documents\Komodo Projects\COP 1000\Module 11\Reverse the Word.py", line 116, in <module> main() File "I:\User\My Documents\Komodo Projects\COP 1000\Module 11\Reverse the Word.py", line 35, in main choice_is_rand(dict_of_strings) File "I:\User\My Documents\Komodo Projects\COP 1000\Module 11\Reverse the Word.py", line 73, in choice_is_rand rand_word = str(dict_of_strings[random.randrange(0,10)]) KeyError: 0

2 个答案:

答案 0 :(得分:0)

public class DispatchOnce {

public static class Token {
    private boolean hasRun = false;
}

private DispatchOnce(){}

public static synchronized void run(Token token, Runnable runnable) {
    if (!token.hasRun) {
        token.hasRun = true;
        runnable.run();
    }
}
}

样本用法

public static DispatchOnce.Token MY_DISPATCH_ONCE_TOKEN = new DispatchOnce.Token();

public void example() {
    DispatchOnce.run(MY_DISPATCH_ONCE_TOKEN, () -> {
        System.out.println("Print once");
    });
}

答案 1 :(得分:0)

i like Vitor approach so i did some modifications to remove unnecessary synchronizations and boost performance:

public class DispatchOnce {

public static class Token {
    private boolean hasRun = false;

    int idx_runner;
    Runner[] runners = new Runner[2];

    interface Runner  {
        public void run(Runnable runnable);
    }

    class Runner1 implements Runner {

        @Override
        public void run(Runnable runnable) {
            synchronized (Token.this) {
                  if (!hasRun) {
                       hasRun = true;
                       idx_runner=1;
                       runnable.run();
                   }
                 }
        }

    }

    class Runner2 implements Runner {

        @Override
        public void run(Runnable runnable) {
        }
    }

     public Token() {
         runners[0] = new Runner1();
         runners[1] = new Runner2();
         idx_runner = 0;
     }  
}

private DispatchOnce(){}

 public static void run(Token token, Runnable runnable) {
        token.runners[token.idx_runner].run(runnable);
  }

}