1.红绿灯
package com.xinbo.thread1;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test_rgb extends Thread{
private static Green g;
private static Red r;
private static Yellow y;
private static SimpleDateFormat slf;
@Override
public void run() {
}
public static void main(String[] args) {
r = new Red();
g = new Green();
y = new Yellow();
try {
y.start();
g.start();
Thread.sleep(100);
r.start();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static class Red extends Thread{
@Override
public void run() {
synchronized (r) {
//try {
//Thread.sleep(50);
//} catch (InterruptedException e) {
//// TODO Auto-generated catch block
//e.printStackTrace();
//}
System.out.println("红灯亮");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (y) {
y.notify();
}
}
}
}
static class Green extends Thread{
@Override
public void run() {
synchronized (g) {
try {
g.wait();
//Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("绿色亮");
}
}
}
static class Yellow extends Thread{
@Override
public void run() {
synchronized (y) {
try {
y.wait();
//Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("黄灯亮");
synchronized (g) {
g.notify();
}
}
}
}
}
2.写一个银行存取款的例子
1.建一个测试类Test
2.建一个Me类继承线程,一个用户类User,User中有属性 姓名name 金钱 money
3.Me类中有构造方法public Me(String name,User u,int x),其中name为线程得名字(即getname()) u 为一个用户,x 为传入用户的金钱(可正可负),
4.在user类中有一个同步方法f(int money),在此方法中输出如下所示
小黄:本次存了5000RMB,卡上余额:105000
中东:本次存了70000RMB,卡上余额:175000
大东:本次取了50000RMB,卡上余额:125000
小东:本次存了5000RMB,卡上余额:130000
中黄:本次取了10000RMB,卡上余额:120000
大黄:本次存了8000RMB,卡上余额:128000
每隔1秒打一行,可以先让卡上钱为10000
package com.xinbo.shengchang;
public class Test4 {
public static void main(String[] args) {
User u=new User("胡",100000);
Me m1=new Me("小胡:",u,5000);
Me m2=new Me("大胡:",u,8000);
Me m3=new Me("中胡:",u,-10000);
Me m4=new Me("小东:",u,5000);
Me m5=new Me("大东:",u,-50000);
Me m6=new Me("中东:",u,70000);
m1.start();m2.start();m3.start();
m4.start();m5.start();m6.start();
}
}
class Me extends Thread{
User u;
int x;//传值给num;
public Me(String name,User u,int x){
super(name);
this.u=u;
this.x=x;
}
@Override
public void run() {
u.f(x);
}
}
class User{
String name;
int money;
public User(String name,int money){
this.name=name;
this.money=money;
}
//银行存取款业务:
public synchronized void f(int num){
try {
Thread.sleep(1000);
money+=num;
if(num<0){
System.out.println(Thread.currentThread().getName()+"本次取了"+Math.abs(num)+"RMB,卡上余额:"+money);
}else{
System.out.println(Thread.currentThread().getName()+"本次存了"+Math.abs(num)+"RMB,卡上余额:"+money);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
3.写两个线程,一个线程打印 1~52,
另一个线程打印字母A-Z。
打印顺序为12A34B56C……5152Z。
public class Test {
/**
* 写两个线程,一个线程打印 1~52,
* 另一个线程打印字母A-Z。
* 打印顺序为12A34B56C……5152Z。
*/
public static void main(String[] args) {
A a=new A();
Num n=new Num(a);
Letter l=new Letter(a);
n.start();l.start();
}
}
class Num extends Thread{
A a;
public Num(A a){
this.a=a;
}
@Override
public void run() {
for(int i=1;i<53;i++){
a.f(i);
}
}
}
class Letter extends Thread{
A a;
public Letter(A a){
this.a=a;
}
@Override
public void run() {
for(char ch='A';ch<='Z';ch++){
a.f(ch);
}
}
}
class A{
int index=1;
public synchronized void f(int a){
while(index%3==0){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print(a+" ");
index++;
this.notify();
}
public synchronized void f(char ch){
while(index%3!=0){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(ch+" ");
index++;
this.notify();
}
}
转载于:https://blog.51cto.com/10079391/1686778