这是怎么了,点android 确定退出就退出了进不去??

19:00 提问
有一道Java面试题,没太理解明白,求大神指点啊~
package test2;
设计4个线程,其中两个线程每次对i增加1,另外两个线程对i每次减少1
@author liuyu
public class Test {
private synchronized void inc(){
System. out .println(Thread.currentThread().getName()+ "--inc--" + i );
private synchronized void dec(){
System. out .println(Thread.currentThread().getName()+ "--dec--" + i );
class Inc implements Runnable {
public void run() {
class Dec extends Thread{
public void run() {
public static void main(String[] args) {
Test t = new Test();
Inc inc = t. new Inc();
// 创建 2 个增加线程
for ( int i = 0; i & 2; i++) {
new Thread(inc);
thread.start();
// 创建 2 个减少线程
for ( int i = 0; i & 2; i++) {
t.new Dec();
thread.start();
输出:Thread-0--inc--1
Thread-3--dec--0
Thread-2--dec---1
Thread-1--inc--0
可是我感觉应该输出是:
Thread-0--inc--1
Thread-1--inc--2
Thread-2--dec--1
Thread-3--dec--0
因为按照执行顺序,当执行第一个for循环第一次的时候创建第一个线程,并且i加1,执行第二次for循环的时候i再加1等于2;
执行第二个for循环的时候,创建第三个线程,并且i减1,执行第二次的后事i再减1等于0
为什么不对呢?
按赞数排序
卤煮的想法没错,程序确实是一步一步从上往下执行的,也创建了四个线程,之所以没有出现预期的结果,可能是有一点被忽略了,java中的线程有自己的生命周期,一个新的线程会经历创建、就绪然后才能进入执行状态。也就是说你的程序创建了四个线程,它们只是进入了就绪状态,等待cpu线程栈的调度,而何时可以得执行以及执行的顺序还要依赖cpu的性能、cpu的个数等等。所以程序的输出结果应该是未知的,卤煮可以多跑几次,应该会输出不同内容。嘿嘿。
这是不是跟线程状态有关系呢?
你只是把线程start了,并不是直接计算了,可能当时线程并没有开始计算。
如果多调用几次应该每次结果都不一样吧?
我只是提供个思路,毕竟我也不是很清楚。sorry
这个你应该能搜到,讲解的就是java的线程同步问题,很经典的套路
我知道例子,我想问的是
为什么每次执行结果顺序不一样呢?我想问的是这个 ,继续求解答,,
四个线程的启动可以认为是同时发生的(间隔时间非常短),线程并发并不代表线程会同时执行(可以百度下并发和并行的区别)。
CPU个数是限制,例如在单核CPU上,同一时间只能将cpu分配给某一个线程执行,这就是所谓的资源竞争,而竞争的结果不一定是相同的。
所以四个线程执行顺序不可预期。
cpu执行哪个,是有时间片的,虽然创建了一个线程,但是线程并不一定会立即被cpu运行,所以可能在等待时间片。
所以也不一定结果就是 1 0 1 0,也有可能是 1 2 1 0
你要想输出结果一定,就需要做线程的同步,保证某个线程先执行,另外一个线程再执行
是的,从程序流程来看,该是你说的结果。但是,线程创建、启动后,并不是马上就执行你的业务逻辑,至于哪个先执行,是需要进行调度的,由于不确定性,结果也是会不一样的。可以借助线程的join()或者优先级等,来提高优先执行顺序,以达到预期效果。
其他相似问题谷歌面试题,求大神帮忙原题是这样的(后面我补充了中文解释):Don'tmindthemapAfterthetraumaofDr.Boolean'slab,therabbitsareeagertoget
谷歌面试题,求大神帮忙
原题是这样的(后面我补充了中文解释):
Don't mind the map
After the trauma of Dr. Boolean's lab, the rabbits are eager to get back to their normal lives in a well-connected community, where they can visit each other frequently. Fortunately, the rabbits learned something about engineering as part of their escape from the lab. To get around their new warren fast, they built an elaborate subway system to connect their holes. Each station has the same number of outgoing subway lines (outgoing tracks), which are numbered.
Unfortunately, sections of warrens are very similar, so they can't tell where they are in the subway system. Their stations have system maps, but not an indicator showing which station the map is in. Needless to say, rabbits get lost in the subway system often. The rabbits adopted an interesting custom to account for this: Whenever they are lost, they take the subway lines in a particular order, and end up at a known station.
For example, say there were three stations A, B, and C, with two outgoing directions, and the stations were connected as follows
Line 1 from A, goes to B. Line 2 from A goes to C.
Line 1 from B, goes to A. Line 2 from B goes to C.
Line 1 from C, goes to B. Line 2 from C goes to A.
Now, suppose you are lost at one of the stations A, B, or C. Independent of where you are, if you take line 2, and then line 1, you always end up at station B. Having a path that takes everyone to the same place is called a meeting path.
We are interested in finding a meeting path which consists of a fixed set of instructions like, 'take line 1, then line 2,' etc. It is possible that you might visit a station multiple times. It is also possible that such a path might not exist. However, subway stations periodically close for maintenance. If a station is closed, then the paths that would normally go to that station, go to the next station in the same direction. As a special case, if the track still goes to the closed station after that rule, then it comes back to the originating station. Closing a station might allow for a meeting path where previously none existed. That is, if you have
A -& B -& C
and station B closes, then you'll have
Alternately, if it was
A -& B -& B
then closing station B yields
Write a function answer(subway) that returns one of:
-1 (minus one): If there is a meeting path without closing a station
The least index of the station to close that allows for a meeting path or
-2 (minus two): If even with closing 1 station, there is no meeting path.
subway will be a list of lists of integers such that subway[station][direction] = destination_station.
That is, the subway stations are numbered 0, 1, 2, and so on. The k^th element of subway (counting from 0) will give the list of stations directly reachable from station k.
The outgoing lines are numbered 0, 1, 2... The r^th element of the list for station k, gives the number of the station directly reachable by taking line r from station k.
Each element of subway will have the same number of elements (so, each station has the same number of outgoing lines), which will be between 1 and 5.
There will be at least 1 and no more than 50 stations.
For example, if
subway = [[2, 1], [2, 0], [3, 1], [1, 0]]
Then one could take the path [1, 0]. That is, from the starting station, take the second direction, then the first. If the first direction was the red line, and the second was the green line, you could phrase this as:
if you are lost, take the green line for 1 stop, then the red line for 1 stop.
So, consider following the directions starting at each station.
0 -& 1 -& 2.
1 -& 0 -& 2.
2 -& 1 -& 2.
3 -& 0 -& 2.
So, no matter the starting station, the path leads to station 2. Thus, for this subway, answer should return -1.
subway = [[1], [0]]
then no matter what path you take, you will always be at a different station than if you started elsewhere. If station 0 closed, that would leave you with
subway = [[0]]
So, in this case, answer would return 0 because there is no meeting path until you close station 0.
To illustrate closing stations,
subway = [[1,1],[2,2],[0,2]]
If station 2 is closed, then
station 1 direction 0 will follow station 2 direction 0 to station 0, which will then be its new destination.
station 1 direction 1 will follow station 2 direction 1 to station 2, but that station is closed, so it will get routed back to station 1, which will be its new destination. This yields
subway = [[1,1],[0,1]]
To provide a Python solution, edit solution.py
To provide a Java solution, edit solution.java
Test cases
(int) subway = [[2, 1], [2, 0], [3, 1], [1, 0]]
(int) subway = [[1, 2], [1, 1], [2, 2]]
大概意思就是,兔子在兔子窝(n &= 50)之间修了地铁,有k(k &= 5)条线路,每条线每一站都有出发,但是不一定每站都到,比如1号线可能是A-&B, B-&A, C-&B; 2号线可能是A-&C, B-&C, C-&A. 由于兔子窝长得都一样,所以兔子经常不知道自己究竟在哪一站,所以它们想了个办法,在任何一站,只要按照某种顺序乘坐地铁,最终都会到达某一站,比如上面的线路,只要按照先坐一号线,再坐二号线的顺序,无论在ABC哪一站出发,最后都会到C站。现在给出一种地铁线路,问存不存在这样一种顺序,满足按照这个顺序坐地铁,从任何一站出发都能到达某一站。以及如果有一站地铁关门维修了(如果某站关门了,那么到达这一站的车这一站不停,直接前往下一站,如果这一站的下一站还是自己,那么就在上一站循环),是否还存在这样一种顺序。
想了两天了,暴力解的话2^50严重超时,跪求大神帮忙
solved, thanks
【云栖快讯】支撑千亿营收,阿里如何做研发?淘宝如何做敏捷实践?如何面对开发中的“黑天鹅”事件?6月29日首届阿里研发效能嘉年华,独家直播,赶紧预约吧!&&
为您提供简单高效、处理能力可弹性伸缩的计算服务,帮助您快速构建更稳定、安全的应用,提升运维效率,降低 IT 成本...
RDS是一种稳定可靠、可弹性伸缩的在线数据库服务。支持MySQL、SQL Server、PostgreSQL、高...2016年11月 Java大版内专家分月排行榜第三2016年6月 Java大版内专家分月排行榜第三2016年4月 Java大版内专家分月排行榜第三
2017年2月 Java大版内专家分月排行榜第二2017年1月 Java大版内专家分月排行榜第二2016年7月 Java大版内专家分月排行榜第二
2016年12月 Java大版内专家分月排行榜第三2016年10月 Java大版内专家分月排行榜第三2016年8月 Java大版内专家分月排行榜第三
2017年2月 Java大版内专家分月排行榜第二2017年1月 Java大版内专家分月排行榜第二2016年7月 Java大版内专家分月排行榜第二
2016年12月 Java大版内专家分月排行榜第三2016年10月 Java大版内专家分月排行榜第三2016年8月 Java大版内专家分月排行榜第三
2016年11月 Java大版内专家分月排行榜第三2016年6月 Java大版内专家分月排行榜第三2016年4月 Java大版内专家分月排行榜第三
2016年11月 Java大版内专家分月排行榜第三2016年6月 Java大版内专家分月排行榜第三2016年4月 Java大版内专家分月排行榜第三
2016年11月 Java大版内专家分月排行榜第三2016年6月 Java大版内专家分月排行榜第三2016年4月 Java大版内专家分月排行榜第三
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。

我要回帖

更多关于 弹窗点击确定后退出 的文章

 

随机推荐