java序列中的服务(线程)
我有一个任务,使3(a,B,C)服务相互依赖。当服务A启动时,服务B可以启动,当服务B启动时,服务C可以启动,当C停止时,B可以停止,当B停止时,A可以停止。 我已设法启动线程,并使用状态选项从一个线程切换到另一个线程。我不得不说,我对java知之甚少,但我刚刚开始学习java,所以我是这方面的新手,所以任何帮助、建议等都会很好 而且我有3个几乎相同的类,所以谁能告诉他们我如何用一个类替换这3个类?有办法吗 这是我的密码:java序列中的服务(线程),java,multithreading,service,Java,Multithreading,Service,我有一个任务,使3(a,B,C)服务相互依赖。当服务A启动时,服务B可以启动,当服务B启动时,服务C可以启动,当C停止时,B可以停止,当B停止时,A可以停止。 我已设法启动线程,并使用状态选项从一个线程切换到另一个线程。我不得不说,我对java知之甚少,但我刚刚开始学习java,所以我是这方面的新手,所以任何帮助、建议等都会很好 而且我有3个几乎相同的类,所以谁能告诉他们我如何用一个类替换这3个类?有办法吗 这是我的密码: public class service_class { int
public class service_class {
int status=1;
public static void main(String[] args) {
service_class service_class = new service_class();
A1 a=new A1(service_class);
B1 b=new B1(service_class);
C1 c=new C1(service_class);
a.start();
b.start();
c.start();
}
}
class A1 extends Thread{
service_class service_class;
A1(service_class service_class){
this.service_class = service_class;
}
@Override
public void run() {
try{
synchronized (service_class) {
while(service_class.status!=1){
service_class.wait();
}
System.out.print("A started" + "\n");
service_class.status = 2;
service_class.notifyAll();
while(service_class.status!=7){
service_class.wait();
}
System.out.print("A stoped" + "\n");
service_class.status = 1;
service_class.notifyAll();
}
}catch (Exception e) {
System.out.println("Exception 1 :"+e.getMessage());
}
}
}
class B1 extends Thread{
service_class service_class;
B1(service_class service_class){
this.service_class = service_class;
}
@Override
public void run() {
try{
synchronized (service_class) {
while(service_class.status!=2){
service_class.wait();
}
System.out.print("B started " + "\n");
service_class.status = 4;
service_class.notifyAll();
while(service_class.status!=6){
service_class.wait();
}
System.out.print("B stoped" + "\n");
service_class.status = 7;
service_class.notifyAll();
}
}catch (Exception e) {
System.out.println("Exception 2 :"+e.getMessage());
}
}
}
class C1 extends Thread{
service_class service_class;
C1(service_class service_class){
this.service_class = service_class;
}
@Override
public void run() {
try{
synchronized (service_class) {
while(service_class.status!=4){
service_class.wait();
}
System.out.print("C started" + "\n");
service_class.status = 5;
service_class.notifyAll();
while(service_class.status!=5){
service_class.wait();
}
System.out.print("C stoped" + "\n");
service_class.status = 6;
service_class.notifyAll();
}
}catch (Exception e) {
System.out.println("Exception 4 :"+e.getMessage());
};
}
}
使用
使用给定的计数初始化countdownlock
。wait
方法块,直到由于调用了countDown()
方法(由其他线程调用),计数达到零,然后释放所有等待的线程。我的建议是编写一个超类:
- 提供初始计数为1的闩锁
- 接受该类的另一个实例或要在执行之前等待的
CountDownLatch
- 在启动时减少其闩锁
- 将该逻辑包装在
中,并提供一个抽象方法run
,其中将实现实际代码innerRun
abstract class LatchedRunnable extends Runnable { private CountDownLatch latch=new CountDownLatch(1); private CountDownLatch wait; public Foo(LatchedRunnable waitFor) { this.wait=waitFor.latch; } public Foo(CountDownLatch waitFor) { this.wait=waitFor; } final run () { //wait for the other thread if (wait!=null) try {wait.await();} catch (InterruptedException e) {return;} //signal that we have started latch.countDown(); //actually start innerRun(); } protected abstract void innerRun(); //do stuff here } class Foo extends LatchedRunnable { Foo(LatchedRunnable waitFor) {super(waitFor);} protected void innerRun() {...} } class Bar extends LatchedRunnable { Bar(LatchedRunnable waitFor) {super(waitFor);} protected void innerRun() {...} } Foo foo = new Foo(null); Bar bar = new Bar(foo);
countdownlock
。wait
方法块,直到由于调用了countDown()
方法(由其他线程调用),计数达到零,然后释放所有等待的线程。我的建议是编写一个超类:
- 提供初始计数为1的闩锁
- 接受该类的另一个实例或要在执行之前等待的
CountDownLatch
- 在启动时减少其闩锁
- 将该逻辑包装在
中,并提供一个抽象方法run
,其中将实现实际代码innerRun
abstract class LatchedRunnable extends Runnable { private CountDownLatch latch=new CountDownLatch(1); private CountDownLatch wait; public Foo(LatchedRunnable waitFor) { this.wait=waitFor.latch; } public Foo(CountDownLatch waitFor) { this.wait=waitFor; } final run () { //wait for the other thread if (wait!=null) try {wait.await();} catch (InterruptedException e) {return;} //signal that we have started latch.countDown(); //actually start innerRun(); } protected abstract void innerRun(); //do stuff here } class Foo extends LatchedRunnable { Foo(LatchedRunnable waitFor) {super(waitFor);} protected void innerRun() {...} } class Bar extends LatchedRunnable { Bar(LatchedRunnable waitFor) {super(waitFor);} protected void innerRun() {...} } Foo foo = new Foo(null); Bar bar = new Bar(foo);
A
、B
和C
之间的区别似乎是:
- 打印的名称字符串,以及
- 每个测试和设置的状态值
final
实例变量,并用传递给(统一)类构造函数的值初始化它们
然而 扩展
线程
通常被认为是个坏主意。首先,这使得使用线程池变得很困难。更好的方法是使用标准的Thread
类,并在构建时向其传递一个Runnable
实例。事实上,如果您正在使用线程池或和执行器服务或其他什么,您甚至不需要自己创建和管理线程
至于wait/notify内容,更容易使用更高级别的同步构造(例如CountDownLatch
)
我有3门几乎相同的课,所以有人能告诉他们我如何用一门课替换这3门课吗?有办法吗
3类A
、B
和C
之间的区别似乎是:
- 打印的名称字符串,以及
- 每个测试和设置的状态值
final
实例变量,并用传递给(统一)类构造函数的值初始化它们
然而 扩展
线程
通常被认为是个坏主意。首先,这使得使用线程池变得很困难。更好的方法是使用标准的Thread
类,并在构建时向其传递一个Runnable
实例。事实上,如果您正在使用线程池或和执行器服务或其他什么,您甚至不需要自己创建和管理线程
至于wait/notify内容,使用更高级别的同步构造(例如
CountDownLatch
)更容易。CountDownLatch,在我的理解中,是一种在不进入死锁的情况下实现同步的机制。就这样
请考虑,Thread1执行的任务是读取文件。一旦读取了完整的文件,另一个线程就可以处理文件内容并获取某些信息。现在,第三个线程负责将信息复制到数据库
假设有多个客户端使用上述相同步骤,并且顺序相同:
ThreadPool<FileReaderThread> poolA;
ThreadPool<FileProcessorThread> poolB;
ThreadPool<DBUpdate> poolC;
ThreadPool-poolA;
线程池池B;
ThreadPool-poolC;
当新请求传入时,将创建一个具有适当计数的countdownlatch。当来自poolA的线程完成其工作时,计数将递减。一旦此计数达到0,将调用poolB中的线程。类似地,另一个countdownlatch将用于同步poolB和poolC中的线程。理想情况下,我们使用倒计时锁存器实现顺序过程
如果有错误,请更正。据我所知,CountDownLatch是一种在不进入死锁的情况下实现同步的机制。就这样 请考虑,Thread1执行的任务是读取文件。一旦读取了完整的文件,另一个线程就可以处理文件内容并获取某些信息。现在,第三个线程负责将信息复制到数据库 假设有多个客户端使用上述相同步骤,并且顺序相同:
ThreadPool<FileReaderThread> poolA;
ThreadPool<FileProcessorThread> poolB;
ThreadPool<DBUpdate> poolC;
ThreadPool-poolA;
线程池池B;
ThreadPool-poolC;
作为新的要求