简体   繁体   English

一个有自己线程的类是否也可以有其他类使用的静态方法?

[英]Can a class with its own thread also have static methods that are used by other classes?

I am new to Java and so apologies if this question come off as ignorant.我是 Java 新手,如果这个问题显得无知,我深表歉意。

Let's say I have a class A that implements Runnable, an instance of the class is created in main and given its own thread.假设我有一个实现 Runnable 的类 A,该类的一个实例在 main 中创建并给出了它自己的线程。 Let's say the class holds the blueprint for a Town object, and this town has inhabitants.假设这个类持有 Town 对象的蓝图,并且这个城镇有居民。 Class Town is able to create new inhabitants. Class Town 能够创造新的居民。 It is also able to print the current layout of the town repeatedly.它还可以重复打印当前城镇的布局。

This class then has a run method which performs a task repeatedly (refresh town layout).这个类然后有一个 run 方法,它重复执行一个任务(刷新城镇布局)。 The class also has static methods which I want to be used by other classes (create new inhabitant).该类还具有我希望其他类使用的静态方法(创建新居民)。

I now have class B which has the blueprint for an inhabitant.我现在有 B 级,其中有居民的蓝图。 Therefore there are many instances of that class which run in their own thread, and want to use the static method of class B to reproduce (ie create new inhabitant).因此,该类的许多实例都在自己的线程中运行,并希望使用类 B 的静态方法进行复制(即创建新居民)。

Assume now that both class A and B objects have their threads running.现在假设 A 类和 B 类对象都在运行它们的线程。 If an object of class B (inhabitant) decides to reproduce (use one of the class B static methods), will this result in class A object thread being interrupted?如果 B 类对象(居民)决定复制(使用 B 类静态方法之一),这是否会导致 A 类对象线程被中断?

Ie when an inhabitant wants to reproduce, will the actions of the reproduce static method be in the inhabitants thread?即当居民想要繁殖时,繁殖静态方法的动作会在居民线程中吗?

I have read about multithreading in Java but the topic is difficult for me to understand when actually implementing it.我已经阅读了 Java 中的多线程,但是在实际实现它时,我很难理解这个主题。 Any advice about multithreading in general would be very helpful.一般来说,任何关于多线程的建议都会非常有帮助。 Thank You in advance.先感谢您。

Assume now that both class A and B objects have their threads running.现在假设 A 类和 B 类对象都在运行它们的线程。 If an object of class B (inhabitant) decides to reproduce (use one of the class B static methods), will this result in class A object thread being interrupted?如果 B 类对象(居民)决定复制(使用 B 类静态方法之一),这是否会导致 A 类对象线程被中断?

No. There is no connection between the class or instance a method is part of, and the thread on which it runs.不会。方法所属的类或实例与其运行的线程之间没有联系。 The only association between a thread and a method is that when you call the method, it's run on the currently-executing thread.线程和方法之间的唯一关联是,当您调用该方法时,它运行在当前正在执行的线程上。 If A 's run is running on thread 4452 and you call B.whatever() on thread 4127, it has no effect whatsoever on thread 4452 unless they both try to synchronize on the same object.如果Arun上线运行4452,你叫B.whatever()上线4127,其无论在什么上线4452没有影响,除非他们都尝试同步同一个对象。

Let's say I have a class A that implements Runnable, an instance of the class is created in main and given its own thread.假设我有一个实现 Runnable 的类 A,该类的一个实例在 main 中创建并给出了它自己的线程。

The idea of giving a class a thread has no meaning in Java.给一个类一个线程的想法在 Java 中没有意义。 You give a Thread object a Runnable instance, and when you start() the thread, it executes the given instance's run() method.你给一个Thread对象一个Runnable实例,当你start()线程时,它执行给定实例的run()方法。 Once the thread is running, there is no difference between how it executes the given run() method, and how your program's main thread executes your program's main(String[] args) method.一旦线程开始运行,它如何执行给定的run()方法与程序的主线程如何执行程序的main(String[] args)方法之间没有区别。

...when an inhabitant wants to reproduce, will the actions of the reproduce static method be in the inhabitants thread? ...当一个居民想要复制时,复制静态方法的动作会在居民线程中吗?

The only things that are in a thread are the local variables of the methods that the thread calls.那是一个线程的唯一的事情是,该线程调用的方法的局部变量。

An object can never be in a thread because an object can never be in a variable: Objects are always in the heap, and variables only hold references to them.对象永远不会线程中,因为对象永远不会变量中:对象总是堆中,而变量只保存对它们的引用。

So, what you describe basically is two different threads, each executing different methods, and both of them operating on the same objects with complete disregard for one another.因此,您所描述的基本上是两个不同的线程,每个线程执行不同的方法,并且它们都在完全无视彼此的情况下对相同的对象进行操作。

That's usually a Bad Thing.这通常是一件坏事。

You'll need to insure that the threads synchronize the operations that they perform on shared objects to avoid messing up each other's work.您需要确保线程同步它们对共享对象执行的操作,以避免弄乱彼此的工作。 A good starting place to learn is to work your way through the Java Concurrencty Tutorial: http://docs.oracle.com/javase/tutorial/essential/concurrency/一个很好的学习起点是按照自己的方式学习 Java 并发教程: http : //docs.oracle.com/javase/tutorial/essential/concurrency/

When you're ready to go deeper, the book "Java Concurrency in Practice" is a good next step: https://www.amazon.com/Java-Concurrency-Practice-Brian-Goetz/dp/0321349601/当你准备好深入学习时,《Java Concurrency in Practice》这本书是一个很好的下一步: https : //www.amazon.com/Java-Concurrency-Practice-Brian-Goetz/dp/0321349601/

Usually methods and other code is always executed in the caller thread unless manually redirected in another thread context.通常方法和其他代码总是在调用者线程中执行,除非在另一个线程上下文中手动重定向。

Since you're speaking of static methods they cannot access class objects anyway, if you change class behaviour in your static methods that might be not a good idea and maybe you should not use so many static methods unless for some constant values and then inter thread access is non problematic in most cases.由于您说的是静态方法,它们无论如何都无法访问类对象,如果您更改静态方法中的类行为,这可能不是一个好主意,也许您不应该使用这么多静态方法,除非对于某些常量值然后线程间在大多数情况下,访问没有问题。 But that depends on your code and what you do in those methods...但这取决于您的代码以及您在这些方法中所做的事情......

In Java you can also use the volatile modifier, if you need to have write access to some variables or class members from multiple threads or use a custom synchronized { ... } block or method.在 Java 中,如果您需要对来自多个线程的某些变量或类成员进行写访问,或者使用自定义的synchronized { ... }块或方法,您还可以使用volatile修饰符。

暂无
暂无

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

相关问题 如果线程只能访问具有不同线程创建的具有自己实例的类的静态方法,则该线程将在哪个线程上执行? - If threads only can access static methods to a class with its own instance, created by a different thread, what thread will this execute on? ComperatorFactory使用静态方法还是每个都在自己的类中? - ComperatorFactory using static methods or each in its own class? 一个类可以有自己的专用PrintWriter吗? - Can a class have its own private PrintWriter? 包中的类如何在同一包的静态方法中使用其他类? - How can a class in a package use other classes in the same package's static methods? 实现相同接口的两个类可以在其常用方法中使用其他类的对象作为参数吗? - Can two classes implementing same interface use other class's object as parameter in its common methods? 每个类默认都有自己的线程吗? - Does each class have its own Thread by default? 在java中实现接口的子类可以有自己的方法吗? - Can a subclass implementing an interface in java have its own methods? 订户类及其超类没有带@subscribe批注的公共方法 - Subscriber class and its super classes have no public methods with the @subscribe annotation 在自己的线程中执行类 - execute class in its own thread 每个派生的 class 如何拥有自己的 static 值,同时在 Java 中共享相同的基方法? - How can each derived class can have its own static value, while sharing the same base method in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM