回复: 7
跳转到指定楼层
上一主题 下一主题
收起左侧

[面试经验]请教Dropbox onsite题 setTimeout实现

全局:

2019(4-6月) 码农类General 本科 全职@dropbox - 网上海投 - Onsite  | | Other | 其他

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
请教Dropbox onsite题  setTimeout实现。楼主最近在看丢盒子的面经,有好几个帖子都提到了setTimeout的实现, 我整理了一下这道题的要求:

下周要Onsite了,这道题研究了很久还是没明白具体要做什么,要写成什么样的。请问地里有没有java选手做过这个题或者懂这个题的,麻烦指点一下该怎么做
每条回复都会加米!谢谢大家!

评分

参与人数 1大米 +20 收起 理由
匿名用户-LYCWV + 20

查看全部评分


上一篇:微软OTS
下一篇:Amazon 社招 OA 新鲜出炉
推荐
mdyuki1016 2019-4-19 11:51:40 | 只看该作者
全局:

评分

参与人数 2大米 +4 收起 理由
Aquileolus + 2 给你点个赞!
小狗雪碧 + 2 给你点个赞!

查看全部评分

回复

使用道具 举报

🔗
oio14644 2019-4-18 00:26:06 | 只看该作者
全局:
lz 有联系方式吗 一起讨论

评分

参与人数 1大米 +2 收起 理由
小狗雪碧 + 2 给你点个赞!

查看全部评分

回复

使用道具 举报

无效楼层,该帖已经被删除
🔗
qingli.tamu 2019-4-18 12:29:28 | 只看该作者
全局:

评分

参与人数 1大米 +2 收起 理由
小狗雪碧 + 2 给你点个赞!

查看全部评分

回复

使用道具 举报

🔗
 楼主| 小狗雪碧 2019-4-18 12:35:21 | 只看该作者
全局:
qingli.tamu 发表于 2019-4-18 12:29
check out this one.
https://gist.github.com/yohhoy/6da797689e16f6fe880c84f41f788c66

感谢!最近各种找资料,目前来看应该就是delay queue的实现。
回复

使用道具 举报

🔗
duanj99 2019-4-25 05:22:35 | 只看该作者
全局:
  1. import threading
  2. import time
  3. import heapq

  4. class photos:
  5.     def __init__(self):
  6.         self.q = []
  7.         self.lock = threading.Lock()
  8.         self.condition = threading.Condition(self.lock)

  9.     def put(self, delaytime, val):
  10.         self.condition.acquire()
  11.         print('start', threading.current_thread().name)
  12.         time.sleep(1)
  13.         heapq.heappush(self.q, (delaytime, val))
  14.         self.condition.release()


  15.     def get(self, cb, expire_time):
  16.         self.condition.acquire()
  17.         print('get start', threading.current_thread().name)
  18.         while True:
  19.             exec_time, val = self.q[0]
  20.             while time.time() < exec_time and time.time() < expire_time:
  21.                 time.sleep(3)

  22.             if exec_time > expire_time:
  23.                 print('waiting for too long', exec_time)
  24.                 return

  25.             self.q.pop(0)
  26.             cb(exec_time, val)
  27.             return val


  28.     def callback(self, exec_time, val):
  29.         print(val, 'is executed at', time.time(), 'target', exec_time)


  30. p = photos()
  31. e_time = time.time() + 100
  32. expire_time = time.time() + 10
  33. print(e_time,'e_time')
  34. f1 = threading.Thread(target=p.put, args=[e_time, 31])
  35. f2 = threading.Thread(target=p.get, args=[p.callback, expire_time])

  36. for f in (f1, f2):
  37.     f.setDaemon(True)
  38.     f.start()

  39. for f in (f1, f2):
  40.     f.join()

  41. print('complete')
复制代码


我大概用q实现了一下 delay queue和call back, 以及time out. 同请教我的理解是不是对的
回复

使用道具 举报

🔗
dengzeyu147 2019-7-17 15:48:40 | 只看该作者
全局:
本帖最后由 dengzeyu147 于 2019-7-17 15:50 编辑

忽略 紫薯自护
回复

使用道具 举报

🔗
yulijiayou 2019-8-30 22:27:06 | 只看该作者
全局:
You will need to use promise and future in multi-threaded programming
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表