文章目录
1 一个线程的启动
2 Java多线程基本概念
2.1 一把锁
sycchronized
2.2 两个并
2.2.1 并发
2.2.2 并行
2.3 三个程
2.3.1 进程
2.3.2 线程
2.3.3 管程
2.4 用户线程和守护线程
线程的来回切换
package com.sk.thread;
/**
* 子线程循环10次,接着主线程循环100,接着又回到子线程循环10次,接着再回到主线程又循环100,如此循环50次,请写出程序。
* @author root
*/
public class TraditionalThreadCommunication
{
public static void main(String[] args)
{
Business business=new Business();
new Thread(new Runnable()
{
@Override
public void run()
{
for (int i=0;i<50;i++)
{
try
{
business.sub(i);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}).start();
for(int i=0;i<50;i++)
{
try
{
business.main(i);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
class Business
{
private boolean isShouldSub=true;
public synchronized void sub(int i) throws Exception
{
while(!isShouldSub)
{
this.wait();
}
for(int j=0;j<10;j++)
{
System.out.println("sub thread sequence of "+j+",loop of"+i);
}
isShouldSub=false;
this.notify();
}
public synchronized void main(int i) throws Exception
{
while(isShouldSub)
{
this.wait();
}
for(int j=0;j<100;j++)
{
System.out.println("main thread sequence of "+j+",loop of"+i);
}
isShouldSub=true;
this.notify();
}
}