您的位置:首页 > 编程语言 > Java开发

JavaSE8基础 多线程 通过实现Runnable接口实现

2017-10-08 22:16 232 查看
礼悟:
公恒学思合行悟,尊师重道存感恩。叶见寻根三返一,江河湖海同一体。
虚怀若谷良心主,愿行无悔给最苦。读书锻炼养身心,诚劝且行且珍惜。

os :windows7 x64
jdk:jdk-8u131-windows-x64
ide:Eclipse Oxygen Release (4.7.0)


Runnable接口实现类:

package blog.jizuiku4;

/*
* @author jizuiku
* @version V17.09.27
*/
public class MyRunnableImpl implements Runnable {

@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0, n = 300; i < n; ++i) {
// 以接口的方式实现多线程,应该用 Thread.currentThread() 获取当前线程的各种信息
System.out.println(Thread.currentThread().getName() + " " + i);
}
}

}


测试类:

package blog.jizuiku4;

/*
* @author jizuiku
* @version V17.09.27
*/
public class RunnableDemo {
public static void main(String[] args) {
MyRunnableImpl mri0 = new MyRunnableImpl();
MyRunnableImpl mri1 = new MyRunnableImpl();

Thread t0 = new Thread(mri0, "博客园_");
Thread t1 = new Thread(mri1, "给最苦");

t0.start();
t1.start();
}
}


结果:



sourceCode:

@FunctionalInterface
public interface Runnable {
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see     java.lang.Thread#run()
*/
public abstract void run();
}


API:



Java优秀,值得学习。
学习资源:itcast和itheima视频库。如果您有公开的资源,可以分享给我的话,用您的资源学习也可以。
博文是观看视频后,融入思考写成的。博文好,是老师讲得好。博文坏,是 给最苦 没认真。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐