package day07;
import javax.management.RuntimeErrorException;
public class SycnDemo_01 {
public static void main(String[] args) {
Table table = new Table();
Thread t1 = new Thread(){
public void run(){
while(true){
int b = table.getBean();
Thread.yield();
System.out.println(getName()+":"+b);
}
}
};
Thread t2 = new Thread(){
public void run(){
while(true){
int b1 = table.getBean();
Thread.yield();
System.out.println(getName()+":"+b1);
}
}
};
t1.start();
t2.start();
}
}
class Table{
private int bean = 20;
public synchronized int getBean(){
if(bean == 0){
throw new RuntimeException("没有豆子了!");
}
Thread.yield();
return bean--;
}
}
package day07;
public class SycnDemo_02 {
public static void main(String[] args) {
Shop shop = new Shop();
Thread t1 = new Thread(){
public void run(){
shop.buy();
}
};
Thread t2 = new Thread(){
public void run(){
shop.buy();
}
};
t1.start();
t2.start();
}
}
class Shop{
public void buy(){
try {
Thread name = Thread.currentThread();
System.out.println(name.getName()+"挑衣服");
Thread.sleep(5000);
synchronized (this) {
System.out.println(name.getName()+"试衣服");
Thread.sleep(5000);
}
System.out.println(name.getName()+"结账离开");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package day07;
public class SycnDemo_03 {
public static void main(String[] args) {
Boo b = new Boo();
Thread t1 = new Thread(){
public void run(){
b.method01();
}
};
Thread t2 = new Thread(){
public void run(){
b.method02();
}
};
t1.start();
t2.start();
}
}
class Boo{
Thread name = Thread.currentThread();
public void method01(){
try {
System.out.println(name.getName()+"正在运行A方法");
Thread.sleep(5000);
System.out.println(name.getName()+"正在运行的A方法完毕!");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void method02(){
try {
System.out.println(name.getName()+"正在运行B方法");
Thread.sleep(5000);
System.out.println(name.getName()+"正在运行的B方法完毕!");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package day07;
public class SycnDemo_04 {
public static void main(String[] args) {
Foo f = new Foo();
Thread t1 = new Thread(){
public void run(){
f.mothed01();
}
};
Thread t2 = new Thread(){
public void run(){
f.mothed02();
}
};
t1.start();
t2.start();
}
}
class Foo{
private Object synA = new Object();
private Object synB = new Object();
public void mothed01(){
try {
synchronized(synA){
System.out.println("我是方法1");
Thread.sleep(2000);
mothed02();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void mothed02(){
try {
synchronized(synB){
System.out.println("我是方法2");
Thread.sleep(2000);
mothed01();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package day07;
public class DemoWait_05 {
public static void main(String[] args) {
Thread t = new My();
Thread t2 = new My2();
t.start();
t2.start();
}
}
class My extends Thread{
public static StringBuilder value = new StringBuilder();
public void run(){
String name = Thread.currentThread().getName();
synchronized (value) {
for(int i=0;i<5;i++){
try {
value.wait(10000);
value.append("a");
System.out.println(name+value.toString());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
class My2 extends Thread{
public void run(){
synchronized (My.value) {
for(int i=0;i<2;i++){
try {
Thread.sleep(2000);
System.out.println("呵呵");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
My.value.notifyAll();
}
}
}
package day07;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPoolDemo_06 {
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(2);
for(int i=0;i<5;i++){
Runnable runn = new Runnable(){
public void run(){
try {
String name =Thread.currentThread().getName();
System.out.println(name+"正在运行任务...");
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println("线程池中断...");
}
}
};
pool.execute(runn);
}
pool.shutdownNow();
}
}