如何启动获取文件锁定的并发线程,并相互等待 [英] How to start concurrent threads that acquire a file lock and wait on each other
问题描述
我正在学习 我怎样才能达到我想要的情景?他们为什么不同时开始?我认为 I'm studying the How can I achieve the scenario I'm trying to get at? And why aren't they starting at the same time? I thought the Threads are started with You're not even creating threads actually:
这篇关于如何启动获取文件锁定的并发线程,并相互等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! FileLock
类。我想要做的是启动三个 Threads
,它们将同时运行并访问单个文件。当文件被一个线程锁定时,我想让另外两个线程在释放锁的时候等待轮到他们。但是,当我在下面运行我的代码时,线程甚至不会同时启动 - 只要它们的每个 run()
方法完成。我不明白。
$ p $ public class Main {
public static void main(String []] args){
Main m = new Main();
SomeThread t1 = m.new SomeThread(t1);
SomeThread t2 = m.new SomeThread(t2);
SomeThread t3 = m.new SomeThread(t3);
t1.run();
t3.run();
t2.run();
}
类SomeThread实现Runnable {
字符串名称;
public SomeThread(String s){
name = s;
}
@Override
public void run(){
System.out.println(name +started!);
OtherClass.access(name);
static class OtherClass {
static File file = new File(testfile.txt);
public static void access(String name){
FileChannel channel = null;
FileLock lock = null;
try {
channel = new RandomAccessFile(file,rw)。getChannel();
lock = channel.lock();
System.out.println(locked by+ name);
Thread.sleep(3000);
} catch(Exception e){
// TODO自动生成的catch块
e.printStackTrace();
} finally {
if(lock!= null){
try {
lock.release();
System.out.println(release by+ name);
} catch(IOException e){
// TODO自动生成的catch块
e.printStackTrace();
if(channel!= null){
try {
channel.close();
} catch(IOException e){
// TODO自动生成的catch块
e.printStackTrace();
$ b code $ pre
lock()
方法只会让其他线程访问相同的文件,直到锁被释放。 Thread.run
。 run
将在主线程上依次调用 run
方法。
$ b $实际上,你甚至不创建线程:
pre $ public static void main(String [] args){
Main m = new Main();
Thread t1 = new Thread(m.new SomeThread(t1));
线程t2 =新线程(m.new SomeThread(t2));
线程t3 =新线程(m.new SomeThread(t3));
t1.start();
t2.start();
t3.start();
}
FileLock
class. What I want to do is start three Threads
that will run at the same time and access a single file. While the file is locked by one thread, I want the other two threads to wait for their turn when the lock is released. However, when I run my code below, the threads don't even start all at the same time--they are started one after the other, as soon as each of their run()
methods is finished. I don't understand.public class Main {
public static void main(String[] args) {
Main m = new Main();
SomeThread t1 = m.new SomeThread("t1");
SomeThread t2 = m.new SomeThread("t2");
SomeThread t3 = m.new SomeThread("t3");
t1.run();
t3.run();
t2.run();
}
class SomeThread implements Runnable {
String name;
public SomeThread(String s) {
name = s;
}
@Override
public void run() {
System.out.println(name + " started!");
OtherClass.access(name);
}
}
static class OtherClass {
static File file = new File("testfile.txt");
public static void access(String name) {
FileChannel channel = null;
FileLock lock = null;
try {
channel = new RandomAccessFile(file, "rw").getChannel();
lock = channel.lock();
System.out.println("locked by " + name);
Thread.sleep(3000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (lock != null) {
try {
lock.release();
System.out.println("released by " + name);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (channel != null) {
try {
channel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
lock()
method only makes the other threads accessing the same file wait until the lock is released.Thread.start
, not Thread.run
. run
will just call the run
method sequentially on the main thread.public static void main(String[] args) {
Main m = new Main();
Thread t1 = new Thread(m.new SomeThread("t1"));
Thread t2 = new Thread(m.new SomeThread("t2"));
Thread t3 = new Thread(m.new SomeThread("t3"));
t1.start();
t2.start();
t3.start();
}