java 学习笔记4

时间:2023-03-08 21:32:20

(1)      线程的强制运行

先看线程强制运行的实例 ,再解释程序中是如何让程序强制运行的

public class ThreadTest1 implementsRunnable {
publicvoid run()
{
int i = 0;
for (int x = 0; x < 10; x++)
{
System.out.println(Thread.currentThread().getName()+"--->>"+i++);
}
}
}
public class threadjoin {
publicstatic void main(String[] args) {
ThreadTest1 t= new ThreadTest1();
Thread pp = new Thread(t);
pp.start();
int i=0;
for(int x =0;x<10;x++)
{
if(i == 5)
{
try
{
pp.join();
} catch (Exception e)
{
System.out.println(e.getMessage());
}
}
System.out.println("main Thread"+ i++ );
}
}
}

在程序启动了两个线程,一个是main()线程,一个是 pp 线程。

程序第 15 行,调用 pp 线程对象的 join()方法,在程序的输出结果中可以发现,调

用 join()方法之后,只有 pp 的线程对象在运行,也就是说,join()方法用来强制某

一线程运行。

由此例可知:由上可见,pp 线程中的代码被并入到了 main 线程中,也就是 pp 线程中的代码不

执行完,main 线程中的代码就只能一直等待。查看 JDK 文档可以发现,除了有无参

数的 join 方法外,还有两个带参数的 join 方法,分别是 join(long millis)和 join(long

millis,int nanos),它们的作用是指定合并时间,前者精确到毫秒,后者精确到纳秒,

意思是两个线程合并指定的时间后,又开始分离,回到合并前的状态。

(2)      多线程的同步

同步代码块定义语法

synchronized(对象)

{

需要同步的代码 ;

}

Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码。

一、当两个并发线程访问同一个对象object中的这个synchronized(this)同步代码块时,一个时间内只能有一个线程得到执行。另一个线程必须等待当前线程执行完这个代码块以后才能执行该代码块。

二、然而,当一个线程访问object的一个synchronized(this)同步代码块时,另一个线程仍然可以访问该object中的非synchronized(this)同步代码块。

三、尤其关键的是,当一个线程访问object的一个synchronized(this)同步代码块时,其他线程对object中所有其它synchronized(this)同步代码块的访问将被阻塞。

四、第三个例子同样适用其它同步代码块。也就是说,当一个线程访问object的一个synchronized(this)同步代码块时,它就获得了这个object的对象锁。结果,其它线程对该object对象所有同步代码部分的访问都被暂时阻塞。

注意要点:

1)  当两个并发线程访问同一个对象object中的这个synchronized(this)同步代码块时,一个时间内只能有一个线程得到执行。另一个线程必须等待当前线程执行完这个代码块以后才能执行该代码块。

2) 当一个线程访问object的一个synchronized(this)同步代码块时,另一个线程仍然可以访问该object中的非synchronized(this)同步代码块。

3) 当一个线程访问object的一个synchronized(this)同步代码块时,其他线程对object中所有其它synchronized(this)同步代码块的访问将被阻塞。

4) 当一个线程访问object的一个synchronized(this)同步代码块时,它就获得了这个object的对象锁。结果,其它线程对该object对象所有同步代码部分的访问都被暂时阻塞。

线程同步代码实例:

public class withdra {
private int totalMoney = 0;
public synchronized void getmoney(int money){
synchronized(this)
{
System.out.println("正在执行getmoney()方法!");
int restmoney = totalMoney;
restmoney -= money;
try{
Thread.sleep(1000);
}catch (InterruptedException e)
{
e.printStackTrace();
}
totalMoney =restmoney;
System.out.println(Thread.currentThread().getName()+"提取1000元后的余额为:"+totalMoney);
}
}
public void setMoney()
{
this.totalMoney=9999;
System.out.println(Thread.currentThread().getName()+"把存款设置为:"+totalMoney);
}
publicwithdra(int totalmoney) {
this.totalMoney =totalmoney;
} }
public class FirThread implements Runnable{
privatewithdra wd; publicvoid run() {
wd.getmoney(1000);
}
publicFirThread(withdra wd)
{ this.wd = wd ;}
}
public class secThread implements Runnable{ privatewithdra wd; publicvoid run() {
wd.setMoney();
}
publicsecThread(withdra wd)
{ this.wd = wd ;} } public class TestSyn {
publicstatic void main(String[] args) {
withdrawd = new withdra(4000);
System.out.println("现在的存款为4000");
FirThreadft = new FirThread(wd);
secThreadst = new secThread(wd);
Threadth1 = new Thread(ft,"th1");
Threadth2 = new Thread(ft,"th2");
th1.start();
try{
Thread.sleep(10);
}catch (InterruptedException e) {
e.printStackTrace();
}
th2.start(); } }