资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java线程代码解释,线程的解释

一下java线程代码中 public CustomThread1() { super("[CustomThread1] Thread"); }; 这种定义怎么解释

Thread 有一个构造方法 : Thread(String threadName)。

成都创新互联公司是少有的网站制作、成都网站建设、营销型企业网站、微信小程序、手机APP,开发、制作、设计、买友情链接、推广优化一站式服务网络公司,自2013年起,坚持透明化,价格低,无套路经营理念。让网页惊喜每一位访客多年来深受用户好评

CustomThread1 继承了Thread ,super("[CustomThread1] Thread") 的意思就是执行父类的构造方法,这样线程的名字就固定了,而不是那种随机生成的 [Thread-N],不便于调试与分析。

java 有关于线程代码的解释

wait() 导致当前线程等待,直到其他线程调用此对象的 notify()方法或 notifyAll()方法前,导致当前线程等待,如不调用此对象的唤醒则不再执行。

java线程的经典代码

package threadgroup;

class ThreadDemo3 extends Thread {

private String name;

private int delay;

public ThreadDemo3(String sname, int i_delay) {

name = sname;

delay = i_delay;

}

public void run() {

try {

sleep(delay);

} catch (InterruptedException e) {

}

System.out.println("多线程测试!\n" + name + "\n" + delay);

}

}

public class testMyThread {

public static void main(String[] args) {

ThreadDemo3 th1,th2,th3;

th1 = new ThreadDemo3("线程1", (int) (Math.random() * 900));

th2 = new ThreadDemo3("线程2", (int) (Math.random() * 900));

th3 = new ThreadDemo3("线程3", (int) (Math.random() * 900));

th1.start();

th2.start();

th3.start();

}

}

package threadgroup;

public class threadDemo {

public static void main(String[] args) {

Thread t = Thread.currentThread();

t.setName("你好吗?");

System.out.println("正在进行的Thread是:" + t);

try {

for (int i = 0; i 5; i++) {

System.out.println("我不叫穆继超" + i);

Thread.sleep(3000);

}

} catch (Exception e) {

// TODO: handle exception

System.out.println("Thread has wrong" + e.getMessage());

}

}

}

package threadgroup;

public class threadDemo2 implements Runnable {

public threadDemo2() {

Thread t1 = Thread.currentThread();

t1.setName("第一个主进程");

System.out.println("正在运行" + t1);

Thread t2 = new Thread(this, "");

System.out.println("在创建一个进程");

t2.start();

try {

System.out.println("使他进入第一个睡眠状态");

Thread.sleep(2000);

} catch (InterruptedException e) {

System.out.println("Thread has wrong" + e.getMessage());

}

System.out.println("退出第一个进程");

}

public void run() {

try {

for (int i = 0; i 5; i++) {

System.out.println("进程" + i);

Thread.sleep(3000);

}

} catch (InterruptedException e) {

// TODO: handle exception

System.out.println("Thread has wrong" + e.getMessage());

}

System.out.println("退出第二个进程");

}

public static void main(String[] args) {

new threadDemo2();

}

}

java中线程构造相关代码,求帮忙解释为何如此运行

如上图运行结果(由于里面有线程  所以建议debug一行一行执行看结果)

第一个Thread 直接start 输出的是runnable run

第二个输出的是subthread run 是因为第二个你在{}里面重写了run方法

可以参看例子

在Main类里面我定义了test方法 输出的是test

我直接new Main().test();输出的是test

我要是

new Main(){

public void test() {

System.out.println("test 重写");

}

}.test();

就重写了test方法  所以就会输出 test 重写

你就是现在thread里面定义了  run 然后又重写了  所以输出的subthread run


分享名称:java线程代码解释,线程的解释
文章地址:http://cdkjz.cn/article/hchsjg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220