Soft_Job 板


LINE

不好意思想请教一个作业问题,对大神来说应该不难,但我在一个地方卡住了。需要实现一个用户输入指令,然後运用多执行绪处理背後的运算,回传用户讯息。运算逻辑的部分老师已经写好,不用管他怎麽计算的,用户也不用真的输入东西,用 System.out.println的方式呈现就好, 比如说System.out.println(solution.runCommand("start 10456060"));我需要印出Started 10456060,程式码以及打印结果连结如下,Solution那个类是新加的,功能有实现出来。 https://scalloped-ceres-c7b.notion.site/Thread-a7f5586100724fe2b18ef8d0b6e707ad 我的问题是 private void startCalculation(long n) { Thread t = new Thread(new Runnable() { @Override public void run() { SlowCalculator s = new SlowCalculator(n); try { // s.run(); results.put(n, s.getResult()); } finally { runningCalculations.remove(n); } } }); runningCalculations.put(n, t); t.start(); } 这个方法,又另外new了一个Runnable(),他才能跑,但在SlowCalculator类里已经引用Runnable应该不需要再这样写才对,但又不会修改,因此上来询问,感激不尽。 -- Sent from nPTT on my iPhone X --
QR Code



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 5.151.132.106 (英国)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Soft_Job/M.1678313742.A.DDF.html
1F:→ foreverk: 再看一下SlowCalculator里面的run? 03/09 08:31
2F:→ a12838910: 你可以问问chat 03/09 08:48
3F:→ ssccg: 这边 new Runnable @Override run 是为了在 s.run 外面再包 03/09 09:56
4F:→ ssccg: 别的动作一起跑,就是必要的,跟你的SlowCalculator是不是 03/09 09:59
5F:→ ssccg: Runnable没什麽关系 03/09 09:59
6F:→ ssccg: 要改顶多就是 new Runnable() 换成 new SlowCalculator(n) 03/09 10:00
7F:→ ssccg: SlowCalculator s = new SlowCalculator(n); 删掉 03/09 10:00
8F:→ ssccg: s.run() 改成 super.run(),逻辑上没什麽变化一样是去包 03/09 10:00
9F:→ ssccg: SlowCalculator.run 03/09 10:01
10F:→ ssccg: 这是Decorator pattern,跟thread、runnable没什麽关系 03/09 10:08
11F:→ ssccg: 如果你是想把整个pattern改掉,那就想想你要怎样、在哪去动 03/09 10:13
12F:→ ssccg: results、runningCalculations 03/09 10:13
13F:推 MoonCode: java也太难了 03/09 10:20
14F:推 OriginStar: 原PO可以想成用户可以同时输入多个指令(像是批次处理) 03/09 11:14
15F:→ OriginStar: 或多个用户可以同时输入指令,然後等待处理结果 03/09 11:15
16F:→ OriginStar: 处理过程可以交给别的thread处理,所以看起来会有2层 03/09 11:17
17F:推 dnzteeqrq: 如果SlowCalculator有实作Runnable本身就是Runnable 03/09 11:31
18F:→ OriginStar: 如果是问Thread与Runnable的关系,举例就是在外送平台 03/09 11:32
19F:→ OriginStar: 点餐(Runnable),但实际上要有人送(Thread) 03/09 11:34
20F:→ dnzteeqrq: 要考虑执行序执行完才能 getResult() 03/09 11:35
21F:→ OriginStar: 能跑是因为new了Thread的,不是new Runnable 03/09 11:36
22F:→ alan3100: www.baeldung.com/java-asynchronous-programming 03/09 18:35
23F:→ alan3100: runnable是新增class thread是新增instance 不要搞错了 03/09 18:46
24F:→ cyntasha: 非常感谢各位高手解惑 已在runCommand加上isalive判断 03/10 08:47
25F:→ cyntasha: 写出把new runnable跟void run拿掉感觉能跑的版本 03/10 08:47







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Tech_Job站内搜寻

TOP