首页
\
问答
\
为什么我的DelayQueue不会以错误的顺序延迟和打印?(Why is my DelayQueue not delaying and printing in the wrong order?)
为什么我的DelayQueue不会以错误的顺序延迟和打印?(Why is my DelayQueue not delaying and printing in the wrong order?)
我有以下假设的一个简单的DelayQueue演示。
class DelayedThing implements Delayed { private final long waitUntil; private final String name; public DelayedThing(String name, long wait) { this.name = name; this.waitUntil = System.currentTimeMillis() + wait; System.out.println("DelayedThing(" + name + " wait=" + wait + " until-" + waitUntil); } @Override public long getDelay(TimeUnit unit) { System.out.println(name + " getDelay = " + unit.convert(waitUntil - System.currentTimeMillis(), TimeUnit.MILLISECONDS)); return unit.convert(waitUntil - System.currentTimeMillis(), TimeUnit.MILLISECONDS); } @Override public int compareTo(Delayed o) { long diff = this.getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS); System.out.println(name + ".compareTo(" + o + ") = " + diff); return Long.signum(diff); } @Override public String toString() { return name; } } public void test() throws InterruptedException { BlockingQueue<Delayed> queue = new DelayQueue<>(); queue.add(new DelayedThing("one second", 1000)); queue.add(new DelayedThing("two seconds", 2000)); queue.add(new DelayedThing("half second", 500)); for (Delayed d : queue) { System.out.println(d); } }
但它打印出来
half second two seconds one second
这显然是错的。
I have the following supposedly trivially simple demo of a DelayQueue.
class DelayedThing implements Delayed { private final long waitUntil; private final String name; public DelayedThing(String name, long wait) { this.name = name; this.waitUntil = System.currentTimeMillis() + wait; System.out.println("DelayedThing(" + name + " wait=" + wait + " until-" + waitUntil); } @Override public long getDelay(TimeUnit unit) { System.out.println(name + " getDelay = " + unit.convert(waitUntil - System.currentTimeMillis(), TimeUnit.MILLISECONDS)); return unit.convert(waitUntil - System.currentTimeMillis(), TimeUnit.MILLISECONDS); } @Override public int compareTo(Delayed o) { long diff = this.getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS); System.out.println(name + ".compareTo(" + o + ") = " + diff); return Long.signum(diff); } @Override public String toString() { return name; } } public void test() throws InterruptedException { BlockingQueue<Delayed> queue = new DelayQueue<>(); queue.add(new DelayedThing("one second", 1000)); queue.add(new DelayedThing("two seconds", 2000)); queue.add(new DelayedThing("half second", 500)); for (Delayed d : queue) { System.out.println(d); } }
but it prints
half second two seconds one second
which is obviously wrong.
原文:https://stackoverflow.com/questions/32206562
更新时间:2023-04-06 13:04
最满意答案
您似乎正在针对所有具有相关数量的产品生成所有人的报告; 在大型数据集上,这可能需要一段时间,因为除了数量之外,您没有专门为产品加入产品:
SELECT p.id, p.name, p.gender, f.name, IFNULL(l.quantity,0) AS quantity FROM person_table AS p JOIN product_table AS f LEFT JOIN person_product AS l ON l.person_id = p.id AND l.product_id = f.id ORDER BY p.id, f.name
结果如下:
这或多或少是你追求的吗?
It seems that you're generating a report of all people, against all products with the relevant quantity; on a large data set this could take a while as you're not specifically joining product to person for anything other than quantity:
SELECT p.id, p.name, p.gender, f.name, IFNULL(l.quantity,0) AS quantity FROM person_table AS p JOIN product_table AS f LEFT JOIN person_product AS l ON l.person_id = p.id AND l.product_id = f.id ORDER BY p.id, f.name
Which results in:
Is that more-or-less what you're after?
相关问答
更多-
而不是COALESCE(a.addressid,0) AS addressexists ,请使用CASE : CASE WHEN a.addressid IS NOT NULL THEN 1 ELSE 0 END AS addressexists 或更简单: (a.addressid IS NOT NULL) AS addressexists 这是因为TRUE在MySQL中显示为1 ,并将FALSE为0 。 Instead of COALESCE(a.addressid,0 ...
-
我一般认为LEFT JOIN更容易理解。 SELECT hc.id, u.id, u.name, c.id, c.title FROM courses as c -- must exist LEFT JOIN users as u on 1=1 -- all users, optional LEFT JOIN hidden_courses as hc on hc.user_id=u.id and hc.course_id=c.id -- optional 假设没有用户,那么你想要没有结果吗? 如果是这样, ...
-
MySQL查询三个表(MySQL query for three tables)[2024-01-31]
SELECT a.person_ID, MAX(c.value_ID) value_ID, b.attribute_name , MAX(c.value) Value FROM people a CROSS JOIN attributes b LEFT JOIN `values` c ON a.value_ID = c.value_ID AND ... -
你需要有适当的连接条件,现在你正在进行交叉连接,然后具有where条件。 SELECT cf.client_id, f.file_name, fc.category_name FROM client_files cf LEFT JOIN files f ON cf.file_id = f.ID LEFT JOIN file_categories fc ON fc.ID = f.file_category_id WHERE cf.client_id = 1; You need t ...
-
MySQL外连接三个表(MySQL outer join three tables)[2022-06-09]
我不能评论......你需要这样的东西吗? http://sqlfiddle.com/#!2/046668/9 SELECT a.id_team, a.team_name, b.id_person, b.is_leader, c.name FROM teams as a LEFT OUTER JOIN (teams_has_persons as b INNER JOIN persons as ... -
根据wallyk的建议,我进入了MySQL Workbench,用连接来处理一些事情,我想我终于明白了。 查询最终成为 SELECT `players`.`userid`,`players`.`username`,`players`.`acct_id`,`inventory`.`id` AS `inv_id`,`inventory`.`item_id` AS `inv_item_id`,`inventory`.`amount` AS `inv_amt`,`vault_inventory`.`id` AS ` ...
-
尝试替换此条件Constraints.CONSTRAINT_TYPE = 'PRIMARY KEY' : SELECT t.TABLE_NAME As 'Table Name', Keys.COLUMN_NAME AS 'Primary Key' FROM INFORMATION_SCHEMA.TABLES t left outer join INFORMATION_SCHEMA.TABLE_CONSTRAINTS Constraints on t.TABLE_NAME ...
-
我认为您可以对users表进行第二次加入,以引入处理退款的用户的名称: SELECT r.id, t.fname, t.lname, t.mobile, t.email, t.amount AS transaction_amount, r.amount AS refund_amount, u1.name AS initiated_by, u2.name AS processed_by, ...
-
你应该尝试LEFT OUTER JOIN 左外连接的想法是,如果没有连接伙伴,它将采用左关系的行并使用NULL填充右关系的字段。 还有一个右外连接...... You should try the LEFT OUTER JOIN The idea of an left outer join is that it takes the rows of left relation and fills the fields of the right relation with NULL if there is no ...
-
加入三个mysql表(Joining three mysql tables)[2023-07-22]
1。 SELECT * FROM posts WHERE postid NOT IN ( SELECT post_id FROM views WHERE user_id = 123 ) 要么 SELECT * FROM posts LEFT JOIN views ON views.post_id = posts.postid AND views.user_id = 123 WHERE viewid IS NOT NULL 1. SELECT * FROM posts WHERE ...