Java tutorial
//package com.java2s; /** * Copyright (C) 2014-2016 Regents of the University of California. * @author: Jeff Thompson <jefft0@remap.ucla.edu> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * A copy of the GNU Lesser General Public License is in the file COPYING. */ import java.util.Random; public class Main { private static Random randomNumberGenerator_; /** * Set the library-wide random number generator; this method will only allow the generator to be set once. * Additionally, this method is thread-safe in that it guarantees that only the first caller will be able to set * the generator. * @param randomNumberGenerator the random number generator * @throws UnsupportedOperationException if a user attempts to set the generator a second time */ public static synchronized void setRandom(Random randomNumberGenerator) { if (randomNumberGenerator_ == null) { randomNumberGenerator_ = randomNumberGenerator; } else { throw new UnsupportedOperationException("The random number generator may only be set once"); } } }