简体   繁体   English

具有属性和同步方法的静态类

[英]Static Class with attributes and synchronized methods

I know that there are alot of nearly equal answers here on StackOverFlow but they didn't fit my needs. 我知道在StackOverFlow上有很多几乎相等的答案,但它们不符合我的需求。

Given the following code example: 给出以下代码示例:

(inner class) (内部舱)

static class OutgoingFileTransfer implements Runnable {

File sendThisFile;

outgoingFileTransfer(File sendThis) {
    this.sendThisFile = sendThis;
}

@Override
public synchronized void run() {
    //Send the file...  
}

} }

I know, that the synchronized method will lock the whole class, but only if the run() method has been called. 我知道,同步方法将锁定整个类,但前提是已调用run()方法。 If I do the following: 如果我执行以下操作:

Thread a = new Thread(new OutgoingFileTransfer(new File("c:/.../file1"))).start();
Thread b = new Thread(new OutgoingFileTransfer(new File("c:/.../file2"))).start();

Could it be, that Thread a initialises file1 and BEFORE entering the synchronized method, and then Thread b interrupts, set file2 and joins the run() method? 难道是线程a初始化了file1并在进入同步方法之前,然后线程b中断了,设置了file2并加入了run()方法? This would mean, that file1 never would be sent. 这意味着,永远不会发送file1。 Right? 对?

And is there any way to prevent this? 有什么办法可以防止这种情况?

I know the cheap way: Make everything non static an add a boolean iAmCurrentlySendingAFilePleaseWaitInTheQueueTillTheOtherFileTransferFinished , so that all goes sequential. 我知道一种便宜的方法:使所有内容都为非静态,然后添加一个boolean iAmCurrentlySendingAFilePleaseWaitInTheQueueTillTheOtherFileTransferFinished ,以便所有操作都按顺序进行。 But I think there will be nicer ways to do this. 但是我认为会有更好的方法来做到这一点。 Thanks for your help. 谢谢你的帮助。 :) :)

Thread a = new Thread(new OutgoingFileTransfer(new File("c:/.../file1"))).start();
Thread b = new Thread(new OutgoingFileTransfer(new File("c:/.../file2"))).start();

Creates two different threads with two different OutgoingFileTransfer objects. 使用两个不同的OutgoingFileTransfer对象创建两个不同的线程。 Each will have its own version of sendThisFile and will operate on it. 每个都有其自己的sendThisFile版本并对其进行操作。 Synchronization and locking come into play when multiple threads act on the same object at the same time. 当多个线程同时作用于同一对象时,同步和锁定就起作用了。 In your case, there is no danger of a race condition, the two threads are independent and run parallely. 在您的情况下,不存在争用情况的危险,这两个线程是独立的并且并行运行。

Edit: 编辑:
Static Classes in Java: Java中的静态类:
Java allows only static inner classes, not static top level classes. Java仅允许静态内部类,而不允许静态顶级类。 A static class does not make everything in it static. 静态类不会使其中的所有内容都是静态的。 From The Java tutorials: 从Java教程中:

In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience. 实际上,静态嵌套类在行为上是顶级类,为了包装方便,该顶级类已嵌套在另一个顶级类中。

So a static class can be instantiated like any other class, can have static and non static members line any other class and the behaviour of static and non static members are like any other class. 因此,静态类可以像任何其他类一样实例化,可以在任何其他类中加入静态和非静态成员,并且静态和非静态成员的行为与其他任何类一样。

So in your case, there are two sendThisFile independent attributes from teo independent instances of the class. 因此,在您的情况下, sendThisFile teo独立实例具有两个sendThisFile独立属性。 In this case, the behaviour is same as if OutgoingFileTransfer were a top level (therefore non static) class. 在这种情况下,其行为与OutgoingFileTransfer是顶级(因此是非静态)类的情况相同。

I think the crux here is the misunderstanding of the static qualifier for nested classes. 我认为问题的关键在于对嵌套类的static限定符的误解。

You can only declare those classes as static that are nested in other ones, meaning 您只能将嵌套在其他类中的那些类声明为static类,这意味着

class A {
    static class B {
    }
}

Outer classes can never be static (in this example, A ). 外部类永远不能是静态的(在此示例中为A )。 static for classes means, that the nested class does not need an instance of the outer class around it . 类的static表示嵌套类不需要围绕它的外部类的实例 Meaning if B is static, you can do 意思是如果B是静态的,你可以做

A.B b = new A.B();

if it is not, you have to do: 如果不是,则必须执行以下操作:

A a = new A();
A.B b = new a.B();

This is heavily opposed to the concept of static with members and methods. 这与成员和方法的static概念完全相反。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM