线程
来源:互联网 发布:ios9越狱后必装软件源 编辑:程序博客网 时间:2024/06/16 17:04
public class Demo1_Thread {
/** * @param args * 证明jvm是多线程的 */public static void main(String[] args) { for(int i = 0; i < 100000; i++) { new Demo(); } for(int i = 0; i < 10000; i++) { System.out.println("我是主线程的执行代码"); }}
}
class Demo {
@Overridepublic void finalize() { System.out.println("垃圾被清扫了");}
}
public class Demo2_Thread {
/** * @param args */public static void main(String[] args) { MyThread mt = new MyThread(); //4,创建Thread类的子类对象 mt.start(); //5,开启线程 for(int i = 0; i < 1000; i++) { System.out.println("bb"); }}
}
class MyThread extends Thread { //1,继承Thread
public void run() { //2,重写run方法
for(int i = 0; i < 1000; i++) { //3,将要执行的代码写在run方法中
System.out.println(“aaaaaaaaaaaa”);
}
}
}
public class Demo3_Thread {
/** * @param args */public static void main(String[] args) { MyRunnable mr = new MyRunnable(); //4,创建Runnable的子类对象 //Runnable target = mr; mr = 0x0011 Thread t = new Thread(mr); //5,将其当作参数传递给Thread的构造函数 t.start(); //6,开启线程 for(int i = 0; i < 1000; i++) { System.out.println("bb"); }}
}
class MyRunnable implements Runnable { //1,定义一个类实现Runnable
@Overridepublic void run() { //2,重写run方法 for(int i = 0; i < 1000; i++) { //3,将要执行的代码写在run方法中 System.out.println("aaaaaaaaaaaa"); }}
}
public class Demo4_Thread {
/** * @param args */public static void main(String[] args) { new Thread() { //1,继承Thread类 public void run() { //2,重写run方法 for(int i = 0; i < 1000; i++) { //3,将要执行的代码写在run方法中 System.out.println("aaaaaaaaaaaaaa"); } } }.start(); //4,开启线程 new Thread(new Runnable() { //1,将Runnable的子类对象传递给Thread的构造方法 public void run() { //2,重写run方法 for(int i = 0; i < 1000; i++) { //3,将要执行的代码写在run方法中 System.out.println("bb"); } } }).start(); //4,开启线程}
}
阅读全文
0 0
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- graph slam tutorial 从推到到应用一
- Android中使用http实现注册登录 1
- 放苹果
- 【兴趣书签】教大家不戴眼镜的好方法
- Maven讲解之 坐标
- 线程
- Redis整合Spring结合使用缓存实例
- 谷歌浏览器5.5以上版本无法设置编码问题的解决方案
- Hibernate一对多,多对一
- 利用python进入数据分析之MovieLens_1M数据分析
- ubuntu php 开启pdo扩展
- 创建PDF模板,java添加内容、导出下载PDF
- 响应式网站——兰途科技 项目开发问题及解析(首页index.html)
- Support Vecor Machine(支持向量机)