api: threads, files
This commit is contained in:
1
app/src/main/assets/sample/多线程/原子变量.js
Normal file
1
app/src/main/assets/sample/多线程/原子变量.js
Normal file
@@ -0,0 +1 @@
|
||||
var i = threads.atomic();
|
||||
11
app/src/main/assets/sample/多线程/变量可见性实验.js
Normal file
11
app/src/main/assets/sample/多线程/变量可见性实验.js
Normal file
@@ -0,0 +1,11 @@
|
||||
var running = true;
|
||||
|
||||
threads.start(function(){
|
||||
while(running){
|
||||
log("running = true");
|
||||
}
|
||||
});
|
||||
|
||||
sleep(2000);
|
||||
running = false;
|
||||
console.info("running = false");
|
||||
26
app/src/main/assets/sample/多线程/多线程简单示例.js
Normal file
26
app/src/main/assets/sample/多线程/多线程简单示例.js
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
//启动一个线程
|
||||
threads.start(function(){
|
||||
//在线程中每隔1秒打印"线程1"
|
||||
while(true){
|
||||
log("线程1");
|
||||
sleep(1000);
|
||||
}
|
||||
});
|
||||
|
||||
//启动另一个线程
|
||||
threads.start(function(){
|
||||
//在线程中每隔2秒打印"线程1"
|
||||
while(true){
|
||||
log("线程2");
|
||||
sleep(2000);
|
||||
}
|
||||
});
|
||||
|
||||
//在主线程中每隔3秒打印"主线程"
|
||||
for(var i = 0; i < 10; i++){
|
||||
log("主线程");
|
||||
sleep(3000);
|
||||
}
|
||||
//打印100次后退出所有线程
|
||||
threads.shutDownAll();
|
||||
14
app/src/main/assets/sample/多线程/线程启动与关闭.js
Normal file
14
app/src/main/assets/sample/多线程/线程启动与关闭.js
Normal file
@@ -0,0 +1,14 @@
|
||||
|
||||
//启动一个无限循环的线程
|
||||
var thread = threads.start(function(){
|
||||
while(true){
|
||||
log("子线程运行中...");
|
||||
sleep(1000);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
//5秒后关闭线程
|
||||
sleep(5000);
|
||||
thread.interrupt();
|
||||
|
||||
Reference in New Issue
Block a user