集合和集合是一样的吗?(Collection and Set are the same?)
我有一个关于Java中的这两个接口的问题。 Set扩展Collection,但不添加任何内容。 他们完全一样。 我在这里错过了什么吗?
I have a question about those two interfaces in Java. Set extends Collection, but doesn't add anything. They are exactly the same. Am I missing something here ?
原文:https://stackoverflow.com/questions/5064871
最满意答案
那么,这是一个常见的误解,java程序本质上是单线程的,因为它们不是。 当你启动一个Java程序时,它将在一个Java虚拟机内执行,它启动其他几个线程来执行你的代码。 检查这个漂亮的博客:
http://blog.jamesdbloom.com/JVMInternals.html#jvm_system_threads
在你的情况下,你最重要的是,你启动一个主线程,执行主方法。 从那里你启动两个独立的线程Thread1和Thread2,这两个线程正在计划执行,但你不知道什么时候OS调度程序会被实际执行。 这是不确定的,原因很多:
- 您不知道调度程序使用什么算法来拾取要执行的线程,
- 你不知道你的处理器有多少核心,你的线程可能并行或串行运行
- 即时编译器可能会重新排列和优化您的代码,
- CPU可能会重新安排读取和写入IO以优化代码的执行,
- 您的代码中可能存在导致数据竞争,竞争状况,饥饿等的错误。
Java并发性是一个很难的话题,我发给你的博客条目是一个很好的开始,随它去吧。 对于严肃的阅读去这里http://www.amazon.com/Java-Concurrency-Practice-Brian-Goetz/dp/0321349601 。
祝你好运。
Well, this is a common misconception, that java programs are single threaded by nature, because they are not. When you start a java program it's being executed inside a Java Virtual Machine, which starts several other threads to execute your code. Check this nice blog:
http://blog.jamesdbloom.com/JVMInternals.html#jvm_system_threads
In your case you most important is, that you start a main thread, which executes a main method. From there you start two separate threads Thread1 and Thread2, which are being scheduled to be executed, but you don't know when they will be picked up by the OS scheduler to be actually executed. It's not deterministic for many reasons:
- you don't know what algorithm scheduler is using to pick up threads to be executed,
- you don't know how many cores your processors have, your threads might run in parallel or serially
- Just In Time compiler might rearrange and optimise your code,
- CPU might rearrange reads and writes to IO to optimise the execution of your code,
- you might have bugs in your code that lead to data races, race conditions, starvations etc.
Java concurrency is a hard topic and the blog entry that I've sent you is a good place to start, go with it. For serious reading go here http://www.amazon.com/Java-Concurrency-Practice-Brian-Goetz/dp/0321349601.
Good luck.
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
您的glassfish输出表明您的log4j配置不正确。 你有一个log4j.properties文件吗? 尝试添加一个基本内容: log4j.rootLogger=INFO,stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPatter ...
-
通常,用于从面板添加/删除一个或两个组件的代码是: panel.remove(..); panel.add(...); panel.revalidate(); panel.repaint(); // sometimes needed 但是,如果要更换面板上的所有组件,则更好的方法是使用卡布局 。 你已经说过revaliate()后跟repaint()不起作用,所以我唯一的建议是发布你的SSCCE来证明问题。 Generally the code for adding/removing one or two ...
-
java中的线程数组(Array of threads in java)[2021-09-01]
它非常类似。 java代码有一个Runnable接口,而不是C#的Name属性java有setName方法。 除此之外,差异很小: Thread[] TCreate = new Thread[iThreadSize]; for (int i = 0; i < TCreate.length; i++) { TCreate[i] = new Thread(new Runnable() { public void run() { lst.Add(this.getR ... -
运行多个在java中执行不同作业的线程的正确方法是什么?(What is a correct way to run multiple threads that do different jobs in java?)[2023-02-27]
不要在实现Thread或Runnable的类中放置main方法。 你可以用你当前的Server实现来实现你想要的,但是我没有看到这么做的好理由。 分开关注,KISS: 一个无聊的简单类,带有一个public static void main(String[] args)方法 One Server implements Runnable类(一种线程类型) 一种Calculations implements Runnable类(另一种类型的线程) 具有main方法的类将启动Server和Calculations ... -
Java,使用线程(Java, working with threads)[2022-08-24]
在存储上创建一个同步方法,在接受存储时返回true。 像这样的东西... public synchronized boolean store ( int num) { if ( items < capacity ) { items ++; return true; } return false; } Create a synchronised method on storage which returns true on accepting s ... -
试试这个!!! private void insertPanelInPanel(JPanel jpContainer, JPanel jpContained) { jpContainer.removeAll(); jpContainer.setLayout(new java.awt.GridLayout(1,1)); jpContainer.add(jpContained); } Try with this!!! private void insertPanelInPanel(JPanel j ...
-
看看你的put方法,看起来你总是把相同的键( this.point )放在Map ,并改变那个键。 这是错误的,并且会导致相同的键在Map多次出现。 将其更改为: public T put(int row, int column, T value) { Point p = new Point(); p.x = column; p.y = row; this.matrixMap.put(p, value); return value; } 此外,请确保您的Point ...
-
那么,这是一个常见的误解,java程序本质上是单线程的,因为它们不是。 当你启动一个Java程序时,它将在一个Java虚拟机内执行,它启动其他几个线程来执行你的代码。 检查这个漂亮的博客: http://blog.jamesdbloom.com/JVMInternals.html#jvm_system_threads 在你的情况下,你最重要的是,你启动一个主线程,执行主方法。 从那里你启动两个独立的线程Thread1和Thread2,这两个线程正在计划执行,但你不知道什么时候OS调度程序会被实际执行。 这是 ...
-
使用DCOM处理线程(Working with threads with DCOM)[2022-04-28]
1:否。通过使用cricital部分,您可以保证代码一次只能在一个线程中运行; 实际上,任何调用Enter的线程都会挂起 ,直到任何其他运行该代码的线程进入Leave调用。 但这并不意味着它将在主线程中运行(使用GetCurrentThreadID检查) 2:你提到公寓配置,但哪个公寓穿线模型? 这定义了(D)COM何时为您进行线程同步 。 在实践中,COM将使用代理存根和幕后编组来遍历公寓(和网络)边界,除非您选择了多线程公寓,在这种情况下COM将假设组件自己处理线程问题。 如果我理解正确,主表单会冻结' ...