漂亮的线程代码java,编译线程数
求一个JAVA多线程例子,最好有代码,谢谢啦!
package a.b.test;
做网站、成都网站制作介绍好的网站是理念、设计和技术的结合。创新互联拥有的网站设计理念、多方位的设计风格、经验丰富的设计团队。提供PC端+手机端网站建设,用营销思维进行网站设计、采用先进技术开源代码、注重用户体验与SEO基础,将技术与创意整合到网站之中,以契合客户的方式做到创意性的视觉化效果。
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中线程编程代码怎么写啊
线程用到Thread或者Runnable接口(Thread也操作了Runnable接口)
继承了Thread类后需要重载其run方法,在方法里写你需要完成的事情,开始线程是调用其start方法。
操作Runnable接口必须实现其run方法,在方法里写你需要完成的事情,Runnable接口没有start方法,所以启动线程还是需要依靠Thread类 new Thread(Runnable runnable).start();
一般项目中多是操作接口,因为类只能单继承,接口可以操作多个。
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多线程代码,求注释,越详尽越好!有点急,谢谢!
这段代码的功能是显示各个时区当前时钟。
TimerListener是一个接口,有一个timeElapsed方法,目的是根据当前的时间绘制时钟,并刷新显示。
Timer继承Thread类,实现了run方法。run方法中,休眠指定的时间,并调用TimerListener的timeElapsed方法。如上例就是每休眠1S调用一次,所以看到的结果就是每1S绘制的时钟会更新一次。
ClockCanvas继承JPanel并实现了TimerListener接口,在构造方法中,根据指定的时区得到calendar实例。并开启线程Timer。
重写了paintComponent方法,在该方法中,首先绘制了一个圆,然后分别绘制时针、分针和秒针。
时针颜色为红色,分针为黄色,秒针为蓝色。在时钟下面绘制了城市,颜色为黑色。
网站栏目:漂亮的线程代码java,编译线程数
当前URL:http://scyanting.com/article/dsgeogo.html