java线程经典代码,java线程例子

Java多线程经典问题!求代码!最好给我思路

//银行卡类

创新互联长期为上千余家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为晋州企业提供专业的成都网站建设、成都网站制作,晋州网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。

public class BanCard {

private Double money = 5000d;

public synchronized void drawMoney(double howMoney,String threadName){

try {

System.out.println(threadName+"进入取钱操作!");

Thread.sleep(2000);//为了提前是一次只有一个线程进入此方法,进行了睡眠2秒

if(howMoneymoney){

System.out.println(threadName+"余额不足!");

return;

}

this.money-=howMoney;

System.out.println(threadName+"-原始余额:"+this.money+",取钱"+howMoney+"后,还剩余额"+this.money);

System.out.println(threadName+"结束取钱操作!");

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

//线程类

public class ThreadDemo implements Runnable {

private BanCard banCard = new BanCard();

private double howMoney=0d;

public void run() {

banCard.drawMoney(this.howMoney,Thread.currentThread().getName());

}

public double getHowMoney() {

return howMoney;

}

public void setHowMoney(double howMoney) {

this.howMoney = howMoney;

}

}

//main所在类

public class Test1 {

public static void main(String[] args) {

ThreadDemo threadDemo = new ThreadDemo();

threadDemo.setHowMoney(3000d);//取款3000

Thread thread1 = new Thread(threadDemo);

thread1.start();

threadDemo.setHowMoney(4000d);//取款4000

Thread thread2 = new Thread(threadDemo);

thread2.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线程经典代码,java线程例子
网站路径:http://scyanting.com/article/hdcdgs.html