public class Work1 {
public static void main(String[] args) {
new Thread(()->{printMessage("张三");}).start();
new Thread(()->{printMessage("李四");}).start();
new Thread(()->{printMessage("王五");}).start();
new Thread(()->{printMessage("赵六");}).start();
new Thread(()->{printMessage("钱七");}).start();
}
public synchronized static void printMessage(String name){
int i = new Random().nextInt(5)+5;
System.out.println(name+"开始过独⽊桥了");
try {
Thread.sleep(i*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name+"通过独⽊桥了!");
}
}
class MyThread extends Thread{
public void run()
{
try {
Thread.currentThread().sleep(3000);
} catch (InterruptedException e) {
}
System.out.println("MyThread running");
}
}
public class ThreadTest{
public static void main(String argv[])
{
MyThread t = new MyThread();
t.run();
t.start();
System.out.println("Thread Test");
}
}
MyThread running
Thread Test
MyThread running
public static void main(String[] args) {
Random random = new Random();
new Thread(()->{
for (int i = 0; i < 500 ; i++) {
System.out.println(random.nextInt(10));
}
}).start();
new Thread(()->{
for (int i = 0; i < 500 ; i++) {
System.out.println((char) ('a'+random.nextInt(26)));
}
}).start();
}
-
需求说明 总经理今天很忙 任务清单: 开除那个不靠谱的副总经理; 给各部⻔总监开会; 陪VIP客 户吃饭,打牌,KTV,桑拿,按摩…; 去⾹港给妻⼦买个 华为⽜逼版 作为⽣⽇礼物; 去机场接 ⼥⼉送到公司旁边的希尔顿饭店休息; 陪⽼妈去医院看腰间盘突出; 辅导⼉⼦做作业 请帮助总经理⽤多线程的⽅式完成今天的任务
public static void main(String[] args) {
new Thread(()->{
System.out.println("开除那个不靠谱的副总经理;");
}).start();
new Thread(()->{
System.out.println("给各部⻔总监开会;");
}).start();
new Thread(()->{
System.out.println("陪VIP客户吃饭,打牌,KTV,桑拿,按摩...;");
}).start();
new Thread(()->{
System.out.println("去⾹港给妻⼦买个 华为⽜逼版 作为⽣⽇礼物;");
}).start();
new Thread(()->{
System.out.println("去机场接⼥⼉送到公司旁边的希尔顿饭店休息;");
}).start();
new Thread(()->{
System.out.println("陪⽼妈去医院看腰间盘突出;");
}).start();
new Thread(()->{
System.out.println("辅导⼉⼦做作业");
}).start();
}
public class Work5 {
static int i = 0;
public static void main(String[] args) {
Thread thread = new Thread(() -> print1());
Thread thread1 = new Thread(() -> print2());
thread.start();
thread1.start();
}
public synchronized static void print1(){
for (int j = 1; j < 11; j++) {
try {
if (i%2==0) {
System.out.println("print1= "+j);
i++;
Work5.class.notify();
}
else {
j--;
Work5.class.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized static void print2(){
for (int j = 10; j >0; j--) {
try {
if (i%2==1) {
System.out.println("print2= "+j);
i++;
Work5.class.notify();
}
else {
j++;
Work5.class.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
-
当⼀个线程进⼊⼀个对象的⼀个synchronized⽅法后,其它线程是否可进⼊此对象 的其它⽅法?
不能,⼀个对象的⼀个synchronized⽅法只能由⼀个线程访问。
-
请说出你所知道的线程同步的⽅法。 wait():使⼀个线程处于等待状态,并且释放所持有的对象的lock。
sleep():使⼀个正 在运⾏的线程处于睡眠状态,是⼀个静态⽅法,调⽤此⽅法要捕捉 InterruptedException异常。
notify():唤醒⼀个处于等待状态的线程,注意的是在调 ⽤此⽅法的时候,并不能确切的唤醒某⼀个等待状态的线程,⽽是由JVM确定唤醒哪 个线程,⽽且不是按优先级。
Allnotity():唤醒所有处⼊等待状态的线程,注意并不是给所有唤醒线程⼀个对象的锁,⽽是让它们竞争。
-
设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。写出程 序。 注:因为这4个线程共享J,所以线程类要写到内部类中。 加线程:每次对j加 ⼀。 减线程:每次对j减⼀
public class Work8 {
static int j = 0;
static Lock lock = new ReentrantLock();
static Condition conditionAdd = lock.newCondition();
static Condition conditionSub = lock.newCondition();
static boolean isAdd = true;
public static void main(String[] args) {
AddThread addThread1 = new AddThread();
AddThread addThread2 = new AddThread();
SubThread subThread1 = new SubThread();
SubThread subThread2 = new SubThread();
addThread1.start();
addThread2.start();
subThread1.start();
subThread2.start();
}
static class AddThread extends Thread{
@Override
public void run() {
lock.lock();
for (int i = 0; i <= 10 ; i++) {
if(isAdd){
j++;
System.out.println("add"+Thread.currentThread().getName()+" j="+j);
isAdd = !isAdd;
conditionSub.signal();
}else {
try {
conditionAdd.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
conditionSub.signalAll();
lock.unlock();
}
}
static class SubThread extends Thread{
@Override
public void run() {
lock.lock();
for (int i = 0; i <= 10; i++) {
if(!isAdd){
j--;
System.out.println("sub"+Thread.currentThread().getName()+" j="+j);
isAdd = !isAdd;
conditionAdd.signal();
}
else {
try {
conditionSub.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
conditionAdd.signal();
lock.unlock();
}
}
}