Runnable问题一个问题

问题描述

由于平时开发中很少用到多线程,可是最近面试总是问多线程,我答不上来,很是尴尬,自己看了这点方面的知识,有个问题请教下大家:直接上代码了代码:public class RunnableTest2 implements Runnable {public void run() {for (int i = 0; i < 100; i++) {System.out.println(Thread.currentThread().getName() + " for loop "+ i);}synchronized (this) {for (int i = 0; i < 100; i++) {if (i == 50) {System.out.println("当前线程被睡眠"+Thread.currentThread().getName());try {Thread.currentThread().sleep(5000);} catch (Exception e) {e.printStackTrace();}}System.out.println(Thread.currentThread().getName()+ "synchronized for loop " + i);}}}public static void main(String[] args) {RunnableTest2 r1 = new RunnableTest2();RunnableTest2 r2 = new RunnableTest2();Thread t1 = new Thread(r1);Thread t2 = new Thread(r2);t1.start();t2.start();}}print结果:Thread-0 for loop 0Thread-0 for loop 1Thread-1 for loop 0Thread-1 for loop 1Thread-1 for loop 2Thread-1 for loop 3Thread-1 for loop 4Thread-1 for loop 5Thread-1 for loop 6Thread-1 for loop 7Thread-1 for loop 8Thread-1 for loop 9Thread-1 for loop 10Thread-1 for loop 11Thread-1 for loop 12Thread-1 for loop 13Thread-1 for loop 14Thread-1 for loop 15Thread-1 for loop 16Thread-1 for loop 17Thread-1 for loop 18Thread-1 for loop 19Thread-1 for loop 20Thread-1 for loop 21Thread-1 for loop 22Thread-1 for loop 23Thread-1 for loop 24Thread-1 for loop 25Thread-1 for loop 26Thread-1 for loop 27Thread-1 for loop 28Thread-1 for loop 29Thread-1 for loop 30Thread-1 for loop 31Thread-1 for loop 32Thread-1 for loop 33Thread-1 for loop 34Thread-1 for loop 35Thread-1 for loop 36Thread-1 for loop 37Thread-1 for loop 38Thread-1 for loop 39Thread-1 for loop 40Thread-1 for loop 41Thread-1 for loop 42Thread-1 for loop 43Thread-1 for loop 44Thread-1 for loop 45Thread-1 for loop 46Thread-1 for loop 47Thread-1 for loop 48Thread-1 for loop 49Thread-1 for loop 50Thread-1 for loop 51Thread-1 for loop 52Thread-1 for loop 53Thread-1 for loop 54Thread-1 for loop 55Thread-1 for loop 56Thread-1 for loop 57Thread-1 for loop 58Thread-1 for loop 59Thread-1 for loop 60Thread-1 for loop 61Thread-1 for loop 62Thread-1 for loop 63Thread-1 for loop 64Thread-1 for loop 65Thread-1 for loop 66Thread-1 for loop 67Thread-1 for loop 68Thread-1 for loop 69Thread-1 for loop 70Thread-1 for loop 71Thread-1 for loop 72Thread-1 for loop 73Thread-1 for loop 74Thread-1 for loop 75Thread-1 for loop 76Thread-1 for loop 77Thread-1 for loop 78Thread-1 for loop 79Thread-1 for loop 80Thread-1 for loop 81Thread-1 for loop 82Thread-1 for loop 83Thread-1 for loop 84Thread-1 for loop 85Thread-1 for loop 86Thread-1 for loop 87Thread-1 for loop 88Thread-1 for loop 89Thread-1 for loop 90Thread-1 for loop 91Thread-1 for loop 92Thread-1 for loop 93Thread-1 for loop 94Thread-1 for loop 95Thread-1 for loop 96Thread-1 for loop 97Thread-1 for loop 98Thread-1 for loop 99Thread-1synchronized for loop 0Thread-1synchronized for loop 1Thread-1synchronized for loop 2Thread-1synchronized for loop 3Thread-1synchronized for loop 4Thread-1synchronized for loop 5Thread-1synchronized for loop 6Thread-1synchronized for loop 7Thread-1synchronized for loop 8Thread-1synchronized for loop 9Thread-1synchronized for loop 10Thread-1synchronized for loop 11Thread-1synchronized for loop 12Thread-1synchronized for loop 13Thread-1synchronized for loop 14Thread-1synchronized for loop 15Thread-1synchronized for loop 16Thread-1synchronized for loop 17Thread-1synchronized for loop 18Thread-1synchronized for loop 19Thread-1synchronized for loop 20Thread-1synchronized for loop 21Thread-1synchronized for loop 22Thread-1synchronized for loop 23Thread-1synchronized for loop 24Thread-1synchronized for loop 25Thread-1synchronized for loop 26Thread-1synchronized for loop 27Thread-1synchronized for loop 28Thread-1synchronized for loop 29Thread-1synchronized for loop 30Thread-1synchronized for loop 31Thread-1synchronized for loop 32Thread-1synchronized for loop 33Thread-1synchronized for loop 34Thread-1synchronized for loop 35Thread-1synchronized for loop 36Thread-1synchronized for loop 37Thread-1synchronized for loop 38Thread-1synchronized for loop 39Thread-1synchronized for loop 40Thread-1synchronized for loop 41Thread-1synchronized for loop 42Thread-1synchronized for loop 43Thread-1synchronized for loop 44Thread-1synchronized for loop 45Thread-1synchronized for loop 46Thread-1synchronized for loop 47Thread-1synchronized for loop 48Thread-1synchronized for loop 49当前线程被睡眠Thread-1Thread-0 for loop 2Thread-0 for loop 3Thread-0 for loop 4Thread-0 for loop 5Thread-0 for loop 6Thread-0 for loop 7Thread-0 for loop 8Thread-0 for loop 9Thread-0 for loop 10Thread-0 for loop 11Thread-0 for loop 12Thread-0 for loop 13Thread-0 for loop 14Thread-0 for loop 15Thread-0 for loop 16Thread-0 for loop 17Thread-0 for loop 18Thread-0 for loop 19Thread-0 for loop 20Thread-0 for loop 21Thread-0 for loop 22Thread-0 for loop 23Thread-0 for loop 24Thread-0 for loop 25Thread-0 for loop 26Thread-0 for loop 27Thread-0 for loop 28Thread-0 for loop 29Thread-0 for loop 30Thread-0 for loop 31Thread-0 for loop 32Thread-0 for loop 33Thread-0 for loop 34Thread-0 for loop 35Thread-0 for loop 36Thread-0 for loop 37Thread-0 for loop 38Thread-0 for loop 39Thread-0 for loop 40Thread-0 for loop 41Thread-0 for loop 42Thread-0 for loop 43Thread-0 for loop 44Thread-0 for loop 45Thread-0 for loop 46Thread-0 for loop 47Thread-0 for loop 48Thread-0 for loop 49Thread-0 for loop 50Thread-0 for loop 51Thread-0 for loop 52Thread-0 for loop 53Thread-0 for loop 54Thread-0 for loop 55Thread-0 for loop 56Thread-0 for loop 57Thread-0 for loop 58Thread-0 for loop 59Thread-0 for loop 60Thread-0 for loop 61Thread-0 for loop 62Thread-0 for loop 63Thread-0 for loop 64Thread-0 for loop 65Thread-0 for loop 66Thread-0 for loop 67Thread-0 for loop 68Thread-0 for loop 69Thread-0 for loop 70Thread-0 for loop 71Thread-0 for loop 72Thread-0 for loop 73Thread-0 for loop 74Thread-0 for loop 75Thread-0 for loop 76Thread-0 for loop 77Thread-0 for loop 78Thread-0 for loop 79Thread-0 for loop 80Thread-0 for loop 81Thread-0 for loop 82Thread-0 for loop 83Thread-0 for loop 84Thread-0 for loop 85Thread-0 for loop 86Thread-0 for loop 87Thread-0 for loop 88Thread-0 for loop 89Thread-0 for loop 90Thread-0 for loop 91Thread-0 for loop 92Thread-0 for loop 93Thread-0 for loop 94Thread-0 for loop 95Thread-0 for loop 96Thread-0 for loop 97Thread-0 for loop 98Thread-0 for loop 99Thread-0synchronized for loop 0Thread-0synchronized for loop 1Thread-0synchronized for loop 2Thread-0synchronized for loop 3Thread-0synchronized for loop 4Thread-0synchronized for loop 5Thread-0synchronized for loop 6Thread-0synchronized for loop 7Thread-0synchronized for loop 8Thread-0synchronized for loop 9Thread-0synchronized for loop 10Thread-0synchronized for loop 11Thread-0synchronized for loop 12Thread-0synchronized for loop 13Thread-0synchronized for loop 14Thread-0synchronized for loop 15Thread-0synchronized for loop 16Thread-0synchronized for loop 17Thread-0synchronized for loop 18Thread-0synchronized for loop 19Thread-0synchronized for loop 20Thread-0synchronized for loop 21Thread-0synchronized for loop 22Thread-0synchronized for loop 23Thread-0synchronized for loop 24Thread-0synchronized for loop 25Thread-0synchronized for loop 26Thread-0synchronized for loop 27Thread-0synchronized for loop 28Thread-0synchronized for loop 29Thread-0synchronized for loop 30Thread-0synchronized for loop 31Thread-0synchronized for loop 32Thread-0synchronized for loop 33Thread-0synchronized for loop 34Thread-0synchronized for loop 35Thread-0synchronized for loop 36Thread-0synchronized for loop 37Thread-0synchronized for loop 38Thread-0synchronized for loop 39Thread-0synchronized for loop 40Thread-0synchronized for loop 41Thread-0synchronized for loop 42Thread-0synchronized for loop 43Thread-0synchronized for loop 44Thread-0synchronized for loop 45Thread-0synchronized for loop 46Thread-0synchronized for loop 47Thread-0synchronized for loop 48Thread-0synchronized for loop 49当前线程被睡眠Thread-0Thread-1synchronized for loop 50Thread-1synchronized for loop 51Thread-1synchronized for loop 52Thread-1synchronized for loop 53Thread-1synchronized for loop 54Thread-1synchronized for loop 55Thread-1synchronized for loop 56Thread-1synchronized for loop 57Thread-1synchronized for loop 58Thread-1synchronized for loop 59Thread-1synchronized for loop 60Thread-1synchronized for loop 61Thread-1synchronized for loop 62Thread-1synchronized for loop 63Thread-1synchronized for loop 64Thread-1synchronized for loop 65Thread-1synchronized for loop 66Thread-1synchronized for loop 67Thread-1synchronized for loop 68Thread-1synchronized for loop 69Thread-1synchronized for loop 70Thread-1synchronized for loop 71Thread-1synchronized for loop 72Thread-1synchronized for loop 73Thread-1synchronized for loop 74Thread-1synchronized for loop 75Thread-1synchronized for loop 76Thread-1synchronized for loop 77Thread-1synchronized for loop 78Thread-1synchronized for loop 79Thread-1synchronized for loop 80Thread-1synchronized for loop 81Thread-1synchronized for loop 82Thread-1synchronized for loop 83Thread-1synchronized for loop 84Thread-1synchronized for loop 85Thread-1synchronized for loop 86Thread-1synchronized for loop 87Thread-1synchronized for loop 88Thread-1synchronized for loop 89Thread-1synchronized for loop 90Thread-1synchronized for loop 91Thread-1synchronized for loop 92Thread-1synchronized for loop 93Thread-1synchronized for loop 94Thread-1synchronized for loop 95Thread-1synchronized for loop 96Thread-1synchronized for loop 97Thread-1synchronized for loop 98Thread-1synchronized for loop 99Thread-0synchronized for loop 50Thread-0synchronized for loop 51Thread-0synchronized for loop 52Thread-0synchronized for loop 53Thread-0synchronized for loop 54Thread-0synchronized for loop 55Thread-0synchronized for loop 56Thread-0synchronized for loop 57Thread-0synchronized for loop 58Thread-0synchronized for loop 59Thread-0synchronized for loop 60Thread-0synchronized for loop 61Thread-0synchronized for loop 62Thread-0synchronized for loop 63Thread-0synchronized for loop 64Thread-0synchronized for loop 65Thread-0synchronized for loop 66Thread-0synchronized for loop 67Thread-0synchronized for loop 68Thread-0synchronized for loop 69Thread-0synchronized for loop 70Thread-0synchronized for loop 71Thread-0synchronized for loop 72Thread-0synchronized for loop 73Thread-0synchronized for loop 74Thread-0synchronized for loop 75Thread-0synchronized for loop 76Thread-0synchronized for loop 77Thread-0synchronized for loop 78Thread-0synchronized for loop 79Thread-0synchronized for loop 80Thread-0synchronized for loop 81Thread-0synchronized for loop 82Thread-0synchronized for loop 83Thread-0synchronized for loop 84Thread-0synchronized for loop 85Thread-0synchronized for loop 86Thread-0synchronized for loop 87Thread-0synchronized for loop 88Thread-0synchronized for loop 89Thread-0synchronized for loop 90Thread-0synchronized for loop 91Thread-0synchronized for loop 92Thread-0synchronized for loop 93Thread-0synchronized for loop 94Thread-0synchronized for loop 95Thread-0synchronized for loop 96Thread-0synchronized for loop 97Thread-0synchronized for loop 98Thread-0synchronized for loop 99我的理解是:Thread-1先占用了同步块,跑到50的时候,睡了,让给了Thread-0,Thread-0跑完第一个循环,跑到了第二循环的50的时候也睡了,然后他们都开始等,由于Thread-0是后睡了,所以Thread-1先醒,醒了就跑,跑完了Thread-0跑。不知道我的理解对不对。

解决方案

楼主,关于sleep方法,会导致线程睡眠,但是只会让出CPU的时间片,不会让出当前线程的锁的。另外,你创建的是两个Runnable的对象,在类内部synchronized(this) { ....}//这其中的this即当前使用的对象,在两个不同对象执行同一个方法时,不会达到互斥效果的。所以,为了实现一个你要的效果可以wait/notify,在一个线程执行过程中使用wait让出拥有的锁,此时notify的执行会让另一个等待锁的线程获取到,并继续执行。
解决方案二:
1.首先sleep()方法会让当前线程睡眠(停止执行)mills毫秒,线程由运行中的状态进入不可运行状态,睡眠时间过后线程会再进入可运行状态Thread-1 与Thread-0 他们是片段执行而已 没有说那个一定要先执行完 再执行那个我把你的循环修改成10了package cn.com.chinagril.test;public class Test implements Runnable { public void run() { for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName() + " for loop " + i); } synchronized (this) { for (int i = 0; i < 10; i++) { if (i == 50) { System.out.println("当前线程被睡眠"+Thread.currentThread().getName()); try { Thread.currentThread().sleep(5000); } catch (Exception e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() + "synchronized for loop " + i); } } } public static void main(String[] args) { Test r1 = new Test(); Test r2 = new Test(); Thread t1 = new Thread(r1); Thread t2 = new Thread(r2); t1.start(); t2.start(); } }Thread-0 for loop 0Thread-1 for loop 0Thread-0 for loop 1Thread-1 for loop 1Thread-0 for loop 2Thread-1 for loop 2Thread-0 for loop 3Thread-1 for loop 3Thread-0 for loop 4Thread-1 for loop 4Thread-1 for loop 5Thread-1 for loop 6Thread-1 for loop 7Thread-1 for loop 8Thread-0 for loop 5Thread-0 for loop 6Thread-1 for loop 9Thread-0 for loop 7Thread-1synchronized for loop 0Thread-0 for loop 8Thread-1synchronized for loop 1Thread-0 for loop 9Thread-1synchronized for loop 2Thread-0synchronized for loop 0Thread-1synchronized for loop 3Thread-0synchronized for loop 1Thread-1synchronized for loop 4Thread-0synchronized for loop 2Thread-1synchronized for loop 5Thread-0synchronized for loop 3Thread-1synchronized for loop 6Thread-0synchronized for loop 4Thread-1synchronized for loop 7Thread-1synchronized for loop 8Thread-0synchronized for loop 5Thread-1synchronized for loop 9Thread-0synchronized for loop 6Thread-0synchronized for loop 7Thread-0synchronized for loop 8Thread-0synchronized for loop 9

时间: 2024-10-31 21:35:29

Runnable问题一个问题的相关文章

Android中Handler Runnable与Thread的区别详解

转自: http://www.codeceo.com/article/android-handler-runnable-thread.html Android中Handler可以异步控制Runnable,那么这样做于Android中的Thread有什么区别呢?本文将通过多个角度来讲解这个问题,读完此文,相信你会对Android中的Handler Runnable与Thread有一个非常全面的了解. 在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口:Th

Runnable

一.概述 在Android中,为避免造成UI Thread的卡顿,Google推荐奖所有的非UI 的操作都放到后台线程去执行,也就是Work Thread.Runnable是一个接口,实现了这个接口就代表可执行,Thread 有带Runnable相应的构造方法,有时候会涉及Runnable这个接口.当然,Google也提供了一个封装好了的异步线程AsyncTask,避免自己去创建和管理自己的线程池.能力有限,不喜勿喷o, kensoon918@163.com only for feedback.

Android开发笔记之:Handler Runnable与Thread的区别详解_Android

在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口:Thread类是在java.lang包中定义的.一个类只要继承了Thread类同时覆写了本类中的run()方法就可以实现多线程操作了,但是一个类只能继承一个父类,这是此方法的局限.下面看例子: 复制代码 代码如下: package org.thread.demo; class MyThread extends Thread{ private String name; public MyThread(St

Java笔记:多线程

一些概念 现在的操作系统是多任务操作系统.多线程是实现多任务的一种方式. 进程 是指一个内存中运行的应用程序,每个进程都有自己独立的一块内存空间,一个进程中可以启动多个线程.比如在 Windows 系统中,一个运行的 exe 就是一个进程. 线程 是指进程中的一个执行流程,一个进程中可以运行多个线程.比如 java.exe 进程中可以运行很多线程.线程总是属于某个进程,进程中的多个线程共享进程的内存. 同时 执行是人的感觉,在线程之间实际上轮换执行. 多线程间堆空间共享,栈空间独立.堆存的是地址

Handler 系列一:如何使用

Handler 是什么? 为什么需要 Handler? Handler 如何使用? handler 存在的问题及改进? Handler:是一个消息分发对象,进行发送和处理消息,并且其 Runnable 对象与一个线程的 MessageQueue 关联.作用:调度消息,将一个任务切换到某个指定的线程中去执行. 为什么需要 Handler? 子线程不允许访问 UI 假若子线程允许访问 UI,则在多线程并发访问情况下,会使得 UI 控件处于不可预期的状态.传统解决办法:加锁,但会使得UI访问逻辑变的复

Java多线程:“基础篇”02之常用的实现多线程的两种方式

本章,我们学习"常用的实现多线程的2种方式":Thread 和 Runnable. 之所以说是 常用的,是因为通过还可以通过java.util.concurrent包中的线程池来实现多线程.关于线程池的内容, 我们以后会详细介绍:现在,先对的Thread和Runnable进行了解.本章内容包括: Thread和Runnable 的简介 Thread和Runnable的异同点 Thread和Runnable的多线程的示例 转载请注明出处:http://www.cnblogs.com/sk

Java编程那些事儿95——多线程实现方式2

12.2.2实现Runnable接口 一个类如果需要具备多线程的能力,也可以通过实现java.lang.Runnable接口进行实现.按照Java语言的语法,一个类可以实现任意多个接口,所以该种实现方式在实际实现时的通用性要比前面介绍的方式好一些. 使用实现Runnable接口实现多线程的示例代码如下: /** * 测试类 */ public class Test2 { public static void main(String[] args) { //创建对象 MyRunnable mr =

《Java特种兵》5.1 基础介绍

5.1 基础介绍 5.1.1 线程基础 本节内容介绍给那些还没接触过Java线程的朋友希望能有个感性认识. Java线程英文名是Thread所有的Java程序的运行都是在进程中分配线程来处理的.如果是一个main方法则由一个主线程来处理如果不创建自定义线程那么这个程序就是单线程的.如果是Web应用程序那么就由Web容器分配线程来处理在4.4.1节中介绍了在Tomcat源码中是如何分配线程的. 也许在使用main方法写代码时我们感觉不到多线程的存在在Web程序中也感觉不到多线程和自己编写程序有什么

基于 SurfaceView 详解 android 幸运大转盘,附带实例app

基于 SurfaceView 详解 android 幸运大转盘,附带实例app       首先说一下,幸运大转盘,以及SurfaceView是在看了也为大神的博客,才有了比较深刻的理解,当然这里附上这位大神的博客地址:博客地址,有兴趣的话你可以去看看,里面有很多的例子.至于我为什么要写这篇博客?,原因之一:加强自己的理解,原因之二:大神的博客就是大神的博客,跳转的太快,基础不好的,很难理解.还有就是一天在实验室太无聊了,没事写写东西.这里我再来更加基础的分析一下.写的不好,原谅.有什么写的不对