當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java ThreadLocalRandom setSeed()用法及代碼示例

Java ThreadLocalRandom 類的 setSeed() 方法拋出 UnsupportedOperationException。在此生成器中,不支持設置種子。此方法覆蓋 Random 類中的 setSeed。

用法:

public void setSeed(long seed)

參數:

seed:這是初始種子

異常:

UnsupportedOperationException:此異常將始終拋出。

例子1

import java.util.concurrent.ThreadLocalRandom;

public class ThreadLocalRandomSetSeedExample1 {
	public static void main(String args[])
	{
	final ThreadLocalRandom random = ThreadLocalRandom.current();  
    random.setSeed(15); //exception will come as seeding is not allowed in ThreadLocalRandom.
    System.out.println("Seeded Thread Local Random Integer:" + random.nextInt());  
		}
}

輸出:

Exception in thread "main" java.lang.UnsupportedOperationException
	at java.base/java.util.concurrent.ThreadLocalRandom.setSeed(Unknown Source)
	at tests.JavaDateSetTimeExample1.main(ThreadLocalRandomSetSeedExample1.java:8)






相關用法


注:本文由純淨天空篩選整理自 Java ThreadLocalRandom setSeed() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。