标签:dea 锁对象 关键字 类的方法 start rri 成员 ++ 时间
前言:本系列将从零开始讲解java多线程相关的技术,内容参考于《java多线程核心技术》与《java并发编程实战》等相关资料,希望站在巨人的肩膀上,再通过我的理解能让知识更加简单易懂。
public class T1 {
public static void main(String[] args) {
PrivateNum p=new PrivateNum();
MyThread threadA=new MyThread(‘A‘,p);
MyThread threadB=new MyThread(‘B‘,p);
threadA.start();
threadB.start();
}}
class MyThread extends Thread
{
char i;
PrivateNum p;
public MyThread(char i,PrivateNum p)
{
this.i=i;
this.p=p;
}
public void run()
{
p.test(i);
}
}
class PrivateNum
{
public void test( char i)
{
try {
int num=0;
if(i==‘A‘)
{
num=100;
System.out.println("线程A已经设置完毕");
Thread.sleep(1000);
}
else
{
num=200;
System.out.println("线程B已经设置完毕");
}
System.out.println("线程"+i+"的值:"+num);
}
catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}}
}
线程A已经设置完毕
线程B已经设置完毕
线程B的值:200
线程A的值:100
public void test( char i)
{
int num=0;
try {
if(i==‘A‘)
{
num=100;
System.out.println("线程A已经设置完毕");
Thread.sleep(1000);
}
else
{
num=200;
System.out.println("线程B已经设置完毕");
}
System.out.println("线程"+i+"的值:"+num);
}
catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}}
线程B已经设置完毕
线程A已经设置完毕
线程B的值:200
线程A的值:200
int num=0
放到了方法的外面,但是输出的结果却不同,因为这时候线程AB访问的是同一个变量(指向同一个地址),所以这个时候会发生覆盖,同时这里出现了线程安全问题。public synchronized void test( char i)
{
int num=0;
try {
if(i==‘A‘)
{
num=100;
System.out.println("线程A已经设置完毕");
Thread.sleep(1000);
}
else
{
num=200;
System.out.println("线程B已经设置完毕");
}
System.out.println("线程"+i+"的值:"+num);
}
catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}}
线程A已经设置完毕
线程A的值:100
线程B已经设置完毕
线程B的值:200
synchronized
,避免了线程安全问题public class T1 {
public static void main(String[] args) {
PrivateNum p1=new PrivateNum();
PrivateNum p2=new PrivateNum();
MyThread threadA=new MyThread(‘A‘,p1);
MyThread threadB=new MyThread(‘B‘,p2);
threadA.start();
threadB.start();
}}
线程A已经设置完毕
线程B已经设置完毕
线程B的值:200
线程A的值:100
线程A已经设置完毕 线程A的值:100
),这是因为这里是两个锁,创建了p1和p2对象,创建的是两个锁,锁对象不同不造成互斥作用。public class T1 {
public static void main(String[] args) {
PrivateNum p1=new PrivateNum();
MyThread threadA=new MyThread(‘A‘,p1);
MyThread2 threadB=new MyThread2(‘B‘,p1);
threadA.start();
threadB.start();
}}
class MyThread extends Thread
{
char i;
PrivateNum p;
public MyThread(char i,PrivateNum p)
{
this.i=i;
this.p=p;
}
public void run()
{
p.test(i);
}
}
class MyThread2 extends Thread
{
char i;
PrivateNum p;
public MyThread2(char i,PrivateNum p)
{
this.i=i;
this.p=p;
}
public void run()
{
p.test2(i);
}
}
class PrivateNum
{
int num=0;
public void test2(char i)
{
System.out.println("线程"+i+"执行,线程A并没有同步执行");
}
public synchronized void test( char i)
{
try {
if(i==‘A‘)
{
num=100;
System.out.println("线程A已经设置完毕");
Thread.sleep(100);
}
else
{
num=200;
System.out.println("线程B已经设置完毕");
}
System.out.println("线程"+i+"的值:"+num);
}
catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}}
}
线程A已经设置完毕
线程B执行,线程A并没有同步执行
线程A的值:100
线程B的值:200
public synchronized void test2(char i)
{
System.out.println("线程"+i+"执行,线程A同步执行");
}
线程A已经设置完毕
线程A的值:100
线程B执行,线程A同步执行
线程B的值:200
public class T1 {
public static void main(String[] args) {
MyThread3 thread=new MyThread3();
thread.start();
}}
class MyThread3 extends Thread
{
Service s=new Service();
public void run()
{
s.service1();
}
}
class Service
{
public synchronized void service1()
{
System.out.println("服务1并没有被锁住");
service2();
}
public synchronized void service2()
{
System.out.println("服务2并没有被锁住");
service3();
}
public synchronized void service3()
{
System.out.println("服务3并没有被锁住");
}
}
服务1并没有被锁住
服务2并没有被锁住
服务3并没有被锁住
public class T1 {
public static void main(String[] args) {
Service3 s=new Service3();
MyThread4 t1=new MyThread4(s,‘1‘);
MyThread4 t2=new MyThread4(s,‘2‘);
t1.start();
t2.start();
}}
class MyThread4 extends Thread
{
Service3 s;
char name;
public MyThread4(Service3 s,char name)
{
this.s=s;
this.name=name;
}
public void run()
{
s.service(name);
}
}
class Service2
{
public synchronized void service(char name)
{
for (int i = 3; i >0; i--) {
System.out.println(i);
}
}
}
class Service3 extends Service2
{
public void service(char name)
{
for (int i = 5; i >0; i--) {
System.out.println("线程"+name+":"+i);
}
}
}
线程1:5 线程2:5
public class T1 {
public static void main(String[] args) {
Service2 s=new Service2();
MyThread t1=new MyThread(s,‘A‘);
MyThread t2=new MyThread(s,‘B‘);
t1.start();
t2.start();
}
}
class Service2
{
public void service(char name)
{
synchronized(this)
{
for (int i = 3; i >0; i--) {
System.out.println(name+":"+i);
}
}
}
}
class MyThread extends Thread
{
Service2 s=new Service2();
char name;
public MyThread(Service2 s,char name)
{
this.s=s;
this.name=name;
}
public void run()
{
s.service(name);
}
}
A:3
A:2
A:1
B:3
B:2
B:1
class Service2
{
String s=new String("锁");
public void service(char name)
{
synchronized(s)
{
for (int i = 3; i >0; i--) {
System.out.println(name+":"+i);
}
}
}
}
public void service(char name)
{
for (int i = 6; i >3; i--) {
System.out.println(name+":"+i);
}
synchronized(this)
{
for (int i = 3; i >0; i--) {
System.out.println(name+":"+i);
}
}
}
A:6
B:6
A:5
B:5
A:4
B:4
A:3
A:2
A:1
B:3
B:2
B:1
public class T1 {
public static void main(String[] args) {
Service2 s=new Service2();
MyThread t1=new MyThread(s,‘A‘);
MyThread2 t2=new MyThread2(s,‘B‘);
t1.start();
t2.start();
}
}
class Service2
{
public void service(char name)
{
synchronized(this)
{
for (int i = 3; i >0; i--) {
System.out.println(name+":"+i);
}
}
}
public void service2(char name)
{
synchronized(this) {
for (int i = 6; i >3; i--) {
System.out.println(name+":"+i);
}
}
}
}
class MyThread extends Thread
{
Service2 s=new Service2();
char name;
public MyThread(Service2 s,char name)
{
this.s=s;
this.name=name;
}
public void run()
{
s.service(name);
}
}
class MyThread2 extends Thread
{
Service2 s=new Service2();
char name;
public MyThread2(Service2 s,char name)
{
this.s=s;
this.name=name;
}
public void run()
{
s.service2(name);
}
}
A:3
A:2
A:1
B:6
B:5
B:4
class Service2
{
Strign s=new String();
public void service(char name)
{
synchronized(s)
{
for (int i = 3; i >0; i--) {
System.out.println(name+":"+i);
}
}
}
public void service2(char name)
{
synchronized(this) {
for (int i = 6; i >3; i--) {
System.out.println(name+":"+i);
}
}
}
}
public void service(char name)
{
synchronized(this)
{
for (int i = 3; i >0; i--) {
System.out.println(name+":"+i);
}
}
}
public synchronized void service2(char name)
{
for (int i = 6; i >3; i--) {
System.out.println(name+":"+i);
}
}
class Service2
{
public synchronized static void service()
{
for (int i = 3; i >0; i--) {
System.out.println(name+":"+i);
}
}
}
class Service2
{
public static void service()
{
synchronized(Service.class)
{
for (int i = 3; i >0; i--) {
System.out.println(name+":"+i);
}
}
}
}
public class T1 {
public static void main(String[] args) {
Service.Service2 s=new Service.Service2();
Thread t1=new Thread(new Runnable()
{public void run(){s.service();}});
Thread t2=new Thread(new Runnable()
{public void run(){Service.Service2.service2();}});
t1.start();
t2.start();
}
}
class Service{
static class Service2
{
public synchronized void service()
{
for (int i = 20; i >10; i--) {
System.out.println(i);
}
}
public static synchronized void service2()
{
for (int i = 9; i >3; i--) {
System.out.println(i);
}
}
}}
//不同步执行
public class DealThread implements Runnable {
public String username;
public Object lock1 = new Object();
public Object lock2 = new Object();
public void setFlag(String username) {
this.username = username;
}
@Override
public void run() {
if (username.equals("a")) {
synchronized (lock1) {
try {
System.out.println("username = " + username);
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (lock2) {
System.out.println("按lock1->lock2代码顺序执行了");
}
}
}
if (username.equals("b")) {
synchronized (lock2) {
try {
System.out.println("username = " + username);
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (lock1) {
System.out.println("按lock2->lock1代码顺序执行了");
}
}
}
}
}
public class Tee {
public static void main(String[] args) {
try {
RunThread thread = new RunThread();
thread.start();
Thread.sleep(1000);
thread.setRunning(false);
System.out.println("已经赋值为false");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class RunThread extends Thread {
private boolean isRunning = true;
public boolean isRunning() {
return isRunning;
}
public void setRunning(boolean isRunning) {
this.isRunning = isRunning;
}
@Override
public void run() {
System.out.println("进入run了");
while (isRunning == true) {
}
System.out.println("线程被停止了!");
}
}
public class Tee {
public static void main(String[] args) {
MyThread[] mythreadArray = new MyThread[100];
for (int i = 0; i < 100; i++) {
mythreadArray[i] = new MyThread();
}
for (int i = 0; i < 100; i++) {
mythreadArray[i].start();
}
}
class MyThread extends Thread {
volatile public static int count;
private static void addCount() {
for (int i = 0; i < 100; i++) {
count++;
}
System.out.println("count=" + count);
}
@Override
public void run() {
addCount();
}
}
class MyThread extends Thread {
static AtomicInteger count=new AtomicInteger(0);
private static void addCount() {
for (int i = 0; i < 100; i++) {
count.incrementAndGet();
}
System.out.println(count.get());
}
@Override
public void run() {
addCount();
}
}
标签:dea 锁对象 关键字 类的方法 start rri 成员 ++ 时间
原文地址:http://www.cnblogs.com/zhangsongren/p/7275932.html