随笔分类 -  多线程。

摘要:1.创建状态 Thread thread = new Thread(); 在程序中用构造方法创建了一个线程对象后, 新的线程对象便处于新建状态,此时他已经有了相应的内存空间和其他资源,但还处于不可运行状态。 2.就绪状态 新建线程对象后,调用该线程到start()方法就可以启动线程,当线程启动时,线 阅读全文
posted @ 2020-12-21 18:41 一块 阅读(106) 评论(0) 推荐(0)
摘要:1.适合多个相同程序代码的线程去处理同一资源的情况。 2.可以避免由于java的单继承特性带来的局限性。 3.增强了程序的健壮性,代码能够被多个线程共享,代码与数据是独立的,所以,在开发中建议用Runnable接口实现多线程。 代码: public class MyThread3 implement 阅读全文
posted @ 2020-12-19 12:03 一块 阅读(353) 评论(0) 推荐(0)
摘要:public class Demo07 { public static void main(String[] args) { final Printer p = new Printer(); new Thread() { public void run() { while (true) { p.pr 阅读全文
posted @ 2020-07-22 21:34 一块 阅读(167) 评论(0) 推荐(0)
摘要:public class Demo06 { public static void main(String[] args) { Thread t1 = new Thread() { public void run() { for (int i = 0; i < 10; i++) { System.ou 阅读全文
posted @ 2020-07-22 21:05 一块 阅读(855) 评论(0) 推荐(0)
摘要:join(); 当前线程暂停,等待指定的线程执行结束后,当前线程在继续。 join(int); 可以等待指定的毫秒之后继续。 public class Demo05 { public static void main(String[] args) { Thread t1 = new Thread() 阅读全文
posted @ 2020-07-22 20:49 一块 阅读(168) 评论(0) 推荐(0)
摘要:public static void main(String[] args) { Thread t1 = new Thread() { public void run() { for (int i = 0; i < 2; i++) { System.out.println(getName() + " 阅读全文
posted @ 2020-07-22 20:36 一块 阅读(151) 评论(0) 推荐(0)
摘要:public class Demo03 { public static void main(String[] args) throws InterruptedException { Test01(); new Thread(){ public void run(){ for(int i = 0;i< 阅读全文
posted @ 2020-07-20 21:45 一块 阅读(142) 评论(0) 推荐(0)
摘要:public class Demo02 { public static void main(String[] args) { Test01(); new Thread(){ public void run(){ System.out.println(getName()+".......ccccccc 阅读全文
posted @ 2020-07-20 21:30 一块 阅读(1154) 评论(0) 推荐(0)
摘要:public class Demo02 { public static void main(String[] args) {第一种方法: new Thread("构造方法设置线程名字"){ public void run(){ System.out.println(this.getName()+". 阅读全文
posted @ 2020-07-20 21:03 一块 阅读(746) 评论(0) 推荐(0)
摘要:public class Demo01 { public static void main(String[] args) {//方法一: new Thread() { //1.继承Thread类 public void run() { //2.重写run方法 for (int i = 0; i < 阅读全文
posted @ 2020-07-20 20:47 一块 阅读(206) 评论(0) 推荐(0)
摘要:方法一:直接继承Thread类 class Mythread extends Thread { // 1.继承Threadpublic void run() {// 2.重写run方法for (int i = 0; i < 1000; i++) { // 3.将要执行的代码写在run方法中Syste 阅读全文
posted @ 2020-07-15 22:12 一块 阅读(352) 评论(0) 推荐(0)