多线程与同步synchronized


package com.thread.sym;

public class TestThread {

/**
* 设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。
* 同步
* 打印结果:
* Thread-0:1
Thread-1:2
Thread-1:3
Thread-1:4
Thread-1:5
Thread-1:6
Thread-1:7
Thread-1:8
Thread-1:9
Thread-1:10
Thread-1:11
Thread-0:12
Thread-0:13
Thread-0:14
Thread-0:15
Thread-0:16
Thread-0:17
Thread-0:18
Thread-0:19
Thread-0:20
Thread-2:19
Thread-2:18
Thread-2:17
Thread-2:16
Thread-2:15
Thread-2:14
Thread-2:13
Thread-2:12
Thread-2:11
Thread-2:10
Thread-3:9
Thread-3:8
Thread-3:7
Thread-3:6
Thread-3:5
Thread-3:4
Thread-3:3
Thread-3:2
Thread-3:1
Thread-3:0

*/
public int j = 0;

public static void main(String[] args) {
TestThread testThread = new TestThread();
myThread1 my11 = testThread.new myThread1();
myThread1 my12 = testThread.new myThread1();
myThread2 my21 = testThread.new myThread2();
myThread2 my22 = testThread.new myThread2();
Thread tmy11 = new Thread(my11);
Thread tmy12 = new Thread(my12);
Thread tmy21 = new Thread(my21);
Thread tmy22 = new Thread(my22);
tmy11.start();
tmy12.start();
tmy21.start();
tmy22.start();

}

class myThread1 implements Runnable {
/*
* 此方法实现对j增加
*/

@Override
public void run() {
for (int i = 0; i < 10; i++) {
inc();
}

}

}

private class myThread2 implements Runnable {
/*
* 此方法实现对j减少
*/
@Override
public void run() {
for (int i = 0; i < 10; i++) {
dec();
}
}

}
//同步的一种方式:同步方法
public synchronized void inc(){
j++;
System.out.println(Thread.currentThread().getName() + ":" + j);
}
public synchronized void dec(){
j--;
System.out.println(Thread.currentThread().getName() + ":" + j);
}
//同步的另一种方式:同步块
/*public void inc(){
synchronized(this){
j++;
System.out.println(Thread.currentThread().getName() + ":" + j);
}
}
public void dec(){
synchronized(this){
j--;
System.out.println(Thread.currentThread().getName() + ":" + j);
}
}*/
}

package com.thread.sym;

public class TestThread {

/**
* 设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。
* 不同步
* 打印结果:
Thread-0:1
Thread-2:1
Thread-2:0
Thread-1:2
Thread-2:-1
Thread-3:1
Thread-3:-2
Thread-3:-3
Thread-0:2
Thread-3:-4
Thread-2:-1
Thread-1:0
Thread-1:-4
Thread-1:-3
Thread-1:-2
Thread-1:-1
Thread-1:0
Thread-1:1
Thread-1:2
Thread-1:3
Thread-2:-5
Thread-3:-4
Thread-0:-3
Thread-3:1
Thread-2:2
Thread-3:1
Thread-0:2
Thread-3:-1
Thread-2:0
Thread-3:-1
Thread-0:0
Thread-3:-3
Thread-2:-2
Thread-2:-3
Thread-0:-2
Thread-2:-4
Thread-0:-3
Thread-0:-2
Thread-0:-1
Thread-0:0

*/
public int j = 0;

public static void main(String[] args) {
TestThread testThread = new TestThread();
myThread1 my11 = testThread.new myThread1();
myThread1 my12 = testThread.new myThread1();
myThread2 my21 = testThread.new myThread2();
myThread2 my22 = testThread.new myThread2();
Thread tmy11 = new Thread(my11);
Thread tmy12 = new Thread(my12);
Thread tmy21 = new Thread(my21);
Thread tmy22 = new Thread(my22);
tmy11.start();
tmy12.start();
tmy21.start();
tmy22.start();

}

class myThread1 implements Runnable {
/*
* 此方法实现对j增加
*/

@Override
public void run() {
for (int i = 0; i < 10; i++) {
inc();
}

}

}

private class myThread2 implements Runnable {
/*
* 此方法实现对j减少
*/
@Override
public void run() {
for (int i = 0; i < 10; i++) {
dec();
}
}

}
public void inc(){
j++;
System.out.println(Thread.currentThread().getName() + ":" + j);
}
public void dec(){
j--;
System.out.println(Thread.currentThread().getName() + ":" + j);
}
}
/*
* Thread 的一般方法:
* sleep:静态方法,使用方式Thread.sleep(1000),一定会抛出异常,由于是运行在run中,一定要采用catch,而不能用throws捕获
* 采用Thread.interrupt()可以让线程停止,但是不推荐此方法,可以采用flag标识来让线程停止。stop()方式已经废弃
* join:合并线程。myThread myt=new myThread();myt.start();myt.join();这样就相当于和主线程合并,变成了一个线程,按照正常的程序逻辑进行
* yield:高风亮节。只让别的线程先执行一下,过一会还是要执行:
* @Override
public void run() {
for (int i = 0; i < 20; i++) {
if (i % 4 == 0) {
Thread.yield();
}
System.out.println(Thread.currentThread().getName()+":"+i);

}
}
Priority:优先级myThread myt=new myThread();myt.setPriority(Thread.NORM_PRIORITY+3);这样将获得更好的优先,时间片更多
线程同步两种方法:同步方法和同步块
sleep和wait的区别:
1.都是用于对线程的控制,但是sleep不释放同步锁,wait释放同步锁
sleep是Thread的静态方法,可以设置时间(单位为毫秒),如果需要终止,可以设置interrupted或者设置标识来终止;wait是Object的方法,不要设置时间,通过别的线程调用同一对象的notify来终止。
2.wait,notify和notifyAll只能在同步控制方法或者同步控制块里面使用,而sleep可以在任何地方使用。
3.sleep必须捕获异常,而wait,notify和notifyAll不需要捕获异常。

*/
package com.wait.sym;

public class Demo {
public static void main(String[] args) {
ThreadDemo b = new ThreadDemo();
b.start();
System.out.println("b is start....");
synchronized (b) {
try {
System.out.println("Waiting for b to complete...");
// b.wait(3000);
b.wait();
System.out.println("Completed.Now back to main thread");
} catch (InterruptedException e) {
}
}
System.out.println("Total is :" + b.total);
}
}

class ThreadDemo extends Thread {
int total;

public void run() {
try {
Thread.sleep(2000);
synchronized (this) {
System.out.println("ThreadB is running..");
for (int i = 0; i < 10; i++) {
total += i;

// if(i==30){Thread.sleep(3000);} //当 i=30的时候,让当前线程睡眠3秒
System.out.println("total is " + total);
}
this.notify();
}
} catch (InterruptedException e1) {

e1.printStackTrace();
}
}
}
/*打印结果:b is start....
Waiting for b to complete...
ThreadB is running..
total is 0
total is 1
total is 3
total is 6
total is 10
total is 15
total is 21
total is 28
total is 36
total is 45
Completed.Now back to main thread
Total is :45*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值