62,626
社区成员
发帖
与我相关
我的任务
分享
public class Test{
private static boolean mainThread=false;
public static void main(String[] args){
new Thread(new Runnable(){
public void run() {
for(int i=0 ; i<50; i++){
synchronized (Test.class) {
if(mainThread){
try {
Test.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for(int j=0; j<10; j++){
System.out.println(Thread.currentThread().getName()+", i="+i+", j="+j);
}
mainThread=true;
Test.class.notify();
}
}
}
}).start();
for(int i =0; i<50; i++){
synchronized (Test.class) {
if(!mainThread){
try {
Test.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for(int j=0; j<3000; j++){
System.out.println(Thread.currentThread().getName()+", i="+i+", j="+j);
}
mainThread=false;
Test.class.notify();
}
}
}
}
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.TimeUnit;
public class Test{
public static void main(String[] args){
String main = Thread.currentThread().getName();
ReentrantLock lock = new ReentrantLock(true);
Thread thread = new Thread(new Task("Task",lock));
thread.start();
//为了子线程先执行,main线程需要睡眠.
try{
TimeUnit.NANOSECONDS.sleep(1);
}catch(InterruptedException e){
e.printStackTrace();
}
for(int i = 0 ; i < 50 ; i ++){
try{
lock.lock();
System.out.printf("%s:%d\n",main,i);
/*
for(int j = 0 ; j < 100 ; j ++){
System.out.printf("%s:%d:%d\n",main,i,j);
}*/
}finally{
lock.unlock();
}
}
}
}
class Task implements Runnable{
public Task(String name,ReentrantLock lock){
this.name = name;
this.lock = lock;
}
@Override
public void run(){
for(int i = 0 ; i < 50 ; i ++){
try{
lock.lock();
System.out.printf("%s:%d\n",name,i);
/*
for(int j = 0 ; j < 10 ; j ++){
System.out.printf("%s:%d:%d\n",name,i,j);
}
*/
}finally{
lock.unlock();
}
}
}
private String name;
private ReentrantLock lock;
}
public class Test{
static boolean flag = false;
public static void main(String[] args){
Thread thread = new Thread(new Task("Task"));
thread.start();
String main = Thread.currentThread().getName();
for(int i = 0 ; i < 50 ; i ++){
try{
synchronized(Test.class){
while(!flag){
Test.class.wait();
}
System.out.printf("%s:%d\n",main,i);
/*
for(int j = 0 ; j < 100 ; j ++){
System.out.printf("%s:%d:%d\n",main,i,j);
}*/
flag = false;
Test.class.notifyAll();
}
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
class Task implements Runnable{
public Task(String name){
this.name = name;
}
@Override
public void run(){
for(int i = 0 ; i < 50 ; i ++){
try{
synchronized(Test.class){
while(Test.flag){
Test.class.wait();
}
System.out.printf("%s:%d\n",name,i);
/*
for(int j = 0 ; j < 10 ; j ++){
System.out.printf("%s:%d:%d\n",name,i,j);
}*/
Test.flag = true;
Test.class.notifyAll();
}
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
private String name;
}
public class Indicator{
// switcher = 0: main is running; switcher = 1: sub is running
private short switcher;
public void executeMain(){
switcher = 0;
}
public void executeSub(){
switcher = 1;
}
public short getSwitcher(){
return switcher;
}
}
public class SubThread implements Runnable{
private Indicator indicator;
public SubThread(Indicator ind){
this.indicator = ind;
}
public void run(){
int counter = 0;
synchronized(indicator){
try{
for(int i = 0; i < 50; i++){
if(indicator.getSwitcher() == 0){
indicator.wait();
}
for(int j = 0; j < 10; j ++){
System.out.println(Thread.currentThread().getName() + " j = " + j);
}
System.out.println(Thread.currentThread().getName() + " after " + (++counter) + " loops of 50" );
indicator.executeMain();
indicator.notify();
}
}catch(InterruptedException ie){
ie.printStackTrace();
}
}
}
}
public class ThreadSwitching{
public static void main(String[] args){
Indicator ind = new Indicator();
SubThread st = new SubThread(ind);
Thread subThread = new Thread(st);
subThread.start();
int counter = 0;
synchronized(ind){
try{
for(int i = 0; i < 50; i++){
if(ind.getSwitcher() == 1){
ind.wait();
}
for(int j = 0; j < 100; j ++){
System.out.println(Thread.currentThread().getName() + " j = " + j);
}
System.out.println(Thread.currentThread().getName() + " after " + (++counter) + " loops of 50" );
ind.executeSub();
ind.notify();
}
}catch(InterruptedException ie){
ie.printStackTrace();
}
}
}
}