java速度入五._多线程编程基础

首先是线程的创建和jion()方法的使用。

 

class Sleeper extends Thread

{

private int sleeptime;

public Sleeper(String name,int sleepTime)

{

super(name);

sleeptime=sleepTime;

start();

}

public void run()

{

try

{

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

{

System.out.println(getName()+"print"+i);

sleep(sleeptime);

}

}catch(InterruptedException e)

{

System.out.println(getName()+"interrupted");

}

System.out.println(getName()+" over");

}

}

class Joiner extends Thread

{

private Sleeper sleeper;

public Joiner(String name,Sleeper sleeper)

{

super(name);

this.sleeper=sleeper;

start();

}

public void run()

{

try

{

System.out.println(getName()+" Begin");

System.out.println(sleeper.getName()+"begin jion()....");

sleeper.join();

for(int i=5;i>0;i--)

{

System.out.println(getName()+"print:"+i);

}

}catch(Exception e)

{

e.printStackTrace();

}

System.out.println(getName()+"Over!");

}

}

public class SimpleJoin {

 

public static void main(String[] args)

{

Sleeper sleeper1=new Sleeper("Tread A sleeper1",1500),

sleeper2=new Sleeper("Tread A sleeper2",1000);

Joiner joiner=new Joiner("ThreadB",sleeper2);

sleeper1.interrupt();

//System.out.println("Hello");

}

 

}

创建三个Tread,第三个jion了第二个。

运行结果:

Tread A sleeper1print0

Tread A sleeper2print0

Tread A sleeper1interrupted

Tread A sleeper1 over

ThreadB Begin

Tread A sleeper2begin jion()....

Tread A sleeper2print1

Tread A sleeper2print2

Tread A sleeper2print3

Tread A sleeper2print4

Tread A sleeper2 over

ThreadBprint:5

ThreadBprint:4

ThreadBprint:3

ThreadBprint:2

ThreadBprint:1

ThreadBOver!

第二个是线程的共享与互斥问题
class Water
    {
static Object pool=new Object();
static int total=6;  //the volumn of the pool
static int in=3;  //the number of water which pool have 
static int out=0;  //the water drain away
}
class ThreadA extends Thread//drain water class
{
void drain()
{
synchronized(Water.pool)
{
System.out.println("Empty or not?"+isEmpty());
if(isEmpty())
{
try
{
Water.pool.wait();
}catch(InterruptedException e)
{
e.printStackTrace();
}
}
else
{
Water.out++;
System.out.println("Drain out water:"+Water.out);
}
}
}
public void run(){
while(Water.in<Water.total){
if(isEmpty())
System.out.println("Empty");
System.out.println("Drain out water now!");
drain();
try{
sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
public boolean isEmpty()
{
return Water.in==Water.out?true:false;
}
}
class ThreadB extends Thread{
void jswork(){
synchronized(Water.pool){
Water.in++;
Water.pool.notify();
System.out.println("In :"+Water.in);
}
}
public void run(){
while(Water.in<Water.total){
System.out.println("Empty");
jswork();
try{
sleep(3000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
public class pool {
public static void main(String[] args)
{
ThreadA threadA=new ThreadA();
ThreadB threadB=new ThreadB();
threadB.start();
threadA.start();
//System.out.println("Hello!");
}
}
java中线程的同步有两种方法。
1.同步方法
synchronized void fun(){}
标记为synchronized的方法,在它执行完之前,其他标记为synchronized的方法都将被阻塞。
2.同步块
synchronized(someobject)
{}
只有拥有运行代码权限的线程才能运行同步块内的代码。
java中的线程通信
主要用到wait()和notify()方法,和操作系统里的p,v操作类似,interrupt()用于终止进程。
例子说明:池塘的类,两个线程,排水和进水,当排水量和进水量相同时,排水量被挂起,直到进水进程注水完成后,才通知排水进程排水。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值