简体   繁体   English

GOSU中的静态方法和线程安全

[英]Static methods in GOSU and Thread-safety

I have below function in a .gs class, which gets called when accessing specific Claim information - 我在.gs类中具有以下函数,该类在访问特定的Claim信息时会被调用-

public static function testVisibility(claim : Claim) : boolean {
    if(claim.State == ClaimState.TC_OPEN){
        return true;
    }
    else{
        return false;
    }
}

My question - 我的问题 -

a) If two users are accessing their respective Claims information, this function should get called twice - first time it should receive the Claim instance of first user, second time Claim instance of second user. a)如果两个用户正在访问其各自的Claims信息,则应调用此函数两次-第一次应接收第一用户的Claim实例,第二次应接收第二用户的Claim实例。 If the accessing in simultaneous - will two copies of the same function be invoked? 如果同时访问-是否将调用同一功能的两个副本? Should not be the case, as static function is only one copy. 事实并非如此,因为静态功能只是一个副本。 So, if it's one copy, how is thread safety ensured? 那么,如果是一个副本,如何确保线程安全? Will the function be called one-after-another? 该函数将一个接一个地调用吗?

b) Like Java, does Gosu also use Heap to run the static functions? b)和Java一样, Gosu是否也使用Heap运行静态函数?

It seems you are confusing a little about the definition here. 您似乎对这里的定义有些困惑。 Thread-safe is only a mechanism created to protect the integrity of data shared between threads. 线程安全仅是一种用于保护线程之间共享的数据完整性的机制。 Therefore, your example function is thread-safe, no matter if it is static or not. 因此,无论示例函数是否为静态,示例函数都是线程安全的。

a) For the reason mentioned above, there would be no thread-safety problem here, because you are working with 2 different sets of data . a)由于上述原因,这里不会存在线程安全问题,因为您正在使用2组不同的数据

b) Provided that Gosu is built to run on JVM, and produce .class files, I believe for the most part (if not 100%, beside the syntax) it will behave like Java. b)只要Gosu能够在JVM上运行并生成.class文件,我相信在大多数情况下(如果不是100%,除了语法), 它将表现得像Java。

This is a cliche confusion when we start loving any programming language. 当我们开始喜欢任何编程语言时,这都是陈词滥调。

Consider 100 people accessing a web-application exactly at a particular point of time, here as per your doubt, the static variable/function will return/share the content value for all the 100 people. 假设有100个人正好在特定时间访问Web应用程序,那么您可能会怀疑,静态变量/函数将为所有100个人返回/共享内容值。 The fact is, data sharing won't happen here because for each server connection, each separate THREAD is created and the entire application works on that thread (called as one-thread-per-connection). 事实是,这里不会发生数据共享,因为对于每个服务器连接,都会创建每个单独的THREAD,并且整个应用程序都在该线程上工作(称为“每个连接一个线程”)。

SO if have a static/global variable, that particular variable will work on 100 different threads, and content/data of each thread will be secure and cant be accessed from other threads(directly). 因此,如果具有静态/全局变量,则该特定变量将在100个不同的线程上工作,并且每个线程的内容/数据将是安全的,并且不能(直接)从其他线程访问。 This is how web applications works. 这就是Web应用程序的工作方式。

If we need to share some variables/Classes among threads, we have to make it singleton. 如果我们需要在线程之间共享一些变量/类,则必须使其单例。 Eg, For database connections, we don't need to create the connection all the time if already an established connection exists. 例如,对于数据库连接,如果已经存在已建立的连接,则无需一直创建连接。 In that case the connection class will be singleton. 在这种情况下,连接类将为单例。

Hope this make sense. 希望这有意义。 :) :)

-Aravind -阿拉文

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

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