我执行了以下代码:
public class TestMain extends Thread{
public static Runnable getRunnableObject(){
Runnable r = new Runnable(){
@Override
public void run() {
System.out.println("inside runnable");
}
};
return r;
}
public static void main(String args[]){
Thread t2 = new Thread(getRunnableObject());
// t2.start();
System.out.println("name "+t2.getName()+" id "+t2.getPriority()+" class "+t2.getClass()
+" priortity "+t2.getPriority()+" state "+t2.getState()+" alive/dead "+t2.isAlive());
System.out.println("runtime"+Runtime.getRuntime().availableProcessors());
t2.setPriority(MAX_PRIORITY);
System.out.println(t2.getPriority());
t2.setPriority(MIN_PRIORITY);
System.out.println(t2.getPriority());
t2.setPriority(NORM_PRIORITY);
System.out.println(t2.getPriority());
}
}
输出:
name Thread-0 id 5 class class java.lang.Thread priortity 5 state NEW alive/dead false
runtime4
10
1
5
现在,我再次运行它,取消第22行的代码注释。
输出2:
inside runnable
name Thread-0 id 5 class class java.lang.Thread priortity 5 state RUNNABLE alive/dead true
runtime4
5
5
5
您能告诉我为什么在线程上调用start()时不能设置优先级吗?
答案 0 :(得分:1)
这是比赛条件。
当您尝试设置
private encryptAndDecrypt encryptedFile;
...
int[] data = new int[999];
for (int i = 0; i <= 998; i++)
{
data[i] = 0;
}
data[index] = 1;
encryptedFile.saveData(data);
的优先级时,它已经死了。
如果您将System.IO.IOException: Sharing violation on path C:\Users\...\AppData\LocalLow\test\...\AnsweredData.dat
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00251] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/FileStream.cs:320
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) [0x00000] in <filename unknown>:0
at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int)
at System.IO.File.Create (System.String path, Int32 bufferSize) [0x00000] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/File.cs:135
at System.IO.File.Create (System.String path) [0x00000] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/File.cs:130
at encryptAndDecrypt.saveData (System.Int32[] value) [0x0001a] in D:\unity\Joon Team\Joon Project\Assets\Joon Asset\Scripts\EncryptedData Scripts\encryptAndDecrypt.cs:31
UnityEngine.Debug:Log(Object)
encryptAndDecrypt:saveData(Int32[]) (at Assets/Joon Asset/Scripts/EncryptedData Scripts/encryptAndDecrypt.cs:42)
singlePlayerGameManager:saveDatatoFile() (at Assets/Joon Asset/Scripts/Single Player Scene Scripts/singlePlayerGameManager.cs:147)
singlePlayerGameManager:option1Selected() (at Assets/Joon Asset/Scripts/Single Player Scene Scripts/singlePlayerGameManager.cs:86)
UnityEngine.EventSystems.EventSystem:Update()
更改为阻止一段时间,例如:
t2
然后您将获得相同的序列10、1、5。