资讯

精准传达 • 有效沟通

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

java中线程实例代码,java线程例子

java 多线程的例子

多线程实际上就是多个线程同时运行,至于那个先完成是不能确定的。

在岭东等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站设计制作、成都网站设计 网站设计制作按需定制网站,公司网站建设,企业网站建设,品牌网站制作,成都营销网站建设,外贸网站制作,岭东网站建设费用合理。

* @author Rollen-Holt 实现Runnable接口

* */

class hello implements Runnable {

public hello() {

}

public hello(String name) {

 this.name = name;

}

public void run() {

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

     System.out.println(name + "运行     " + i);

 }

}

public static void main(String[] args) {

 hello h1=new hello("线程A");

 Thread demo= new Thread(h1);

 hello h2=new hello("线程B");

 Thread demo1=new Thread(h2);

 demo.start();

 demo1.start();

}

private String name;

}

可能运行结果:

求一个JAVA多线程例子,最好有代码,谢谢啦!

package a.b.test;

import java.util.Date;

import java.util.concurrent.Callable;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Future;

public class Calculate1000 implements CallableInteger{

public Calculate1000(){}

public Calculate1000(int a, int b){

this.a = a;

this.b = b;

}

int a;

int b;

/**

* @param args

* @throws Exception

*/

public static void main(String[] args) throws Exception {

//同步

Calculate1000 ca1 = new Calculate1000();

Date ds1 = new Date();

int result = 0;

for(int i = 1 ; i = 1000 ; i++){

result = ca1.add(i, result);

}

System.out.println(result);

System.out.println("同步用时" + (new Date().getTime() - ds1.getTime()) + "MS");

//异步

Date ds2 = new Date();

result = 0;

ExecutorService es = Executors.newFixedThreadPool(2);

FutureInteger future1 = es.submit(new Calculate1000(1,500));

FutureInteger future2 = es.submit(new Calculate1000(501,1000));

result = future1.get() + future2.get();

System.out.println(result);

System.out.println("异步用时" + (new Date().getTime() - ds2.getTime()) + "MS");

es.shutdown();

}

private int add(int a, int b) throws Exception{

Thread.sleep(10);

return a + b;

}

@Override

public Integer call() throws Exception {

int res = 0;

for(int i = a ; i = b ; i++){

res = this.add(res, i);

}

return res;

}

}

楼主你试一下这段代码行不行,行的话请采纳!

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多线程的示例

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

System.out.println(name+"运行,i="+i);

Thread.sleep(1)

}

for循环这样改就行了,因为输出10个数比较小,当你启动启动第二个线程时第一个线程已经运行完毕,所以两次输出都是顺序输出,要么将i设置很大很大,要么让每次打印暂停一下

Java多线程代码,求注释,越详尽越好!有点急,谢谢!

这段代码的功能是显示各个时区当前时钟。

TimerListener是一个接口,有一个timeElapsed方法,目的是根据当前的时间绘制时钟,并刷新显示。

Timer继承Thread类,实现了run方法。run方法中,休眠指定的时间,并调用TimerListener的timeElapsed方法。如上例就是每休眠1S调用一次,所以看到的结果就是每1S绘制的时钟会更新一次。

ClockCanvas继承JPanel并实现了TimerListener接口,在构造方法中,根据指定的时区得到calendar实例。并开启线程Timer。

重写了paintComponent方法,在该方法中,首先绘制了一个圆,然后分别绘制时针、分针和秒针。

时针颜色为红色,分针为黄色,秒针为蓝色。在时钟下面绘制了城市,颜色为黑色。

java怎么创建一个线程

Java线程类也是一个object类,它的实例都继承自java.lang.Thread或其子类。 可以用如下方式用java中创建一个线程:

Tread thread = new Thread();

执行该线程可以调用该线程的start()方法:

thread.start();

编写线程运行时执行的代码有两种方式:一种是创建Thread子类的一个实例并重写run方法,第二种是创建类的时候实现Runnable接口。接下来我们会具体讲解这两种方法:

创建Thread的子类

创建Thread子类的一个实例并重写run方法,run方法会在调用start()方法之后被执行。例子如下:

public class MyThread extends Thread {

public void run(){

System.out.println("MyThread running");

}

}

可以用如下方式创建并运行上述Thread子类

MyThread myThread = new MyThread();

myTread.start();

一旦线程启动后start方法就会立即返回,而不会等待到run方法执行完毕才返回。就好像run方法是在另外一个cpu上执行一样。当run方法执行后,将会打印出字符串MyThread running。

实现Runnable接口

第二种编写线程执行代码的方式是新建一个实现了java.lang.Runnable接口的类的实例,实例中的方法可以被线程调用。下面给出例子:

public class MyRunnable implements Runnable {

public void run(){

System.out.println("MyRunnable running");

}

}

为了使线程能够执行run()方法,需要在Thread类的构造函数中传入 MyRunnable的实例对象。示例如下:

Thread thread = new Thread(new MyRunnable());

thread.start();

当线程运行时,它将会调用实现了Runnable接口的run方法。上例中将会打印出”MyRunnable running”。


本文标题:java中线程实例代码,java线程例子
链接分享:http://cdkjz.cn/article/phgjii.html
多年建站经验

多一份参考,总有益处

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

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

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220