003JAVA多线程同步与异步方法
package com.skcc.mthread;
站在用户的角度思考问题,与客户深入沟通,找到沙依巴克网站设计与沙依巴克网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都做网站、网站制作、企业官网、英文网站、手机端网站、网站推广、域名注册、网页空间、企业邮箱。业务覆盖沙依巴克地区。
public class MyThread002 {
public MyThread002() {
// TODO Auto-generated constructor stub
}
/*****
* synchronized void work() 同步方法
* void eat() 异步方法
* ***/
public synchronized void work() {
System.out.println(Thread.currentThread().getName() + " synchronized work method executed.");
try {
Thread.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void eat() {
System.out.println(Thread.currentThread().getName() + " asynchronized eat method executed.");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MyThread002 m1 = new MyThread002();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
m1.work();
}
},"t1").start();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
//m1.work();
m1.eat();
}
},"t2").start();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
m1.work();
}
},"t3").start();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
//m1.work();
m1.eat();
}
},"t4").start();
}
}
标题名称:003JAVA多线程同步与异步方法
网页地址:http://ybzwz.com/article/jjghis.html