?public class ThreadDomain18 {
????????public void doLongTimeTask() throws Exception {
????????????for (int i = 0; i < 100; i++) {
????????????????System.out.println(
????????????????????????"nosynchronized threadName = " + Thread.currentThread().getName() + ", i = " + (i + 1));
????????????}
????????????System.out.println();
????????????synchronized (this) {
????????????????for (int i = 0; i < 100; i++) {
????????????????????System.out.println(
????????????????????????????"synchronized threadName = " + Thread.currentThread().getName() + ", i = " + (i + 1));
????????????????}
????????????}
????????}
????}
public class MyThread18 extends Thread {
????????private ThreadDomain18 td;
????????public MyThread18(ThreadDomain18 td) {
????????????this.td = td;
????????}
????????public void run() {
????????????try {
????????????????td.doLongTimeTask();
????????????} catch (Exception e) {
????????????????e.printStackTrace();
????????????}
????????}
????}
public static void main(String[] args){
?? ?ThreadDomain18 td = new ThreadDomain18();
?? ?MyThread18 mt0 = new MyThread18(td);
?? ?MyThread18 mt1 = new MyThread18(td);
?? ?mt0.start();
?? ?mt1.start();
}
synchronized threadName = Thread-1, i = 1synchronized threadName = Thread-1, i = 2nosynchronized threadName = Thread-0, i = 95synchronized threadName = Thread-1, i = 3nosynchronized threadName = Thread-0, i = 96synchronized threadName = Thread-1, i = 4nosynchronized threadName = Thread-0, i = 97synchronized threadName = Thread-1, i = 5nosynchronized threadName = Thread-0, i = 98synchronized threadName = Thread-1, i = 6nosynchronized threadName = Thread-0, i = 99synchronized threadName = Thread-1, i = 7nosynchronized threadName = Thread-0, i = 100...synchronized threadName = Thread-1, i = 98synchronized threadName = Thread-1, i = 99synchronized threadName = Thread-1, i = 100synchronized threadName = Thread-0, i = 1synchronized threadName = Thread-0, i = 2synchronized threadName = Thread-0, i = 3...
?public class ThreadDomain19 {
????????public void serviceMethodA() {
????????????synchronized (this) {
????????????????try {
????????????????????System.out.println("A begin time = " + System.currentTimeMillis());
????????????????????Thread.sleep(2000);
????????????????????System.out.println("A end time = " + System.currentTimeMillis());
????????????????} catch (InterruptedException e) {
????????????????????e.printStackTrace();
????????????????}
????????????}
????????}
????????public void serviceMethodB() {
????????????synchronized (this) {
????????????????System.out.println("B begin time = " + System.currentTimeMillis());
????????????????System.out.println("B end time = " + System.currentTimeMillis());
????????????}
????????}
????}
public class MyThread19_0 extends Thread{
?? ?private ThreadDomain19 td;
?? ?public MyThread19_0(ThreadDomain19 td){
?? ??? ?this.td = td;
?? ?}
?? ?public void run(){
?? ??? ?td.serviceMethodA();
?? ?}
}
public class MyThread19_1 extends Thread{
?? ?private ThreadDomain19 td;
?? ?public MyThread19_1(ThreadDomain19 td){
?? ??? ?this.td = td;
?? ?}
?? ?public void run(){
?? ??? ?td.serviceMethodB();
?? ?}
}
public static void main(String[] args){
?? ?ThreadDomain19 td = new ThreadDomain19();
?? ?MyThread19_0 mt0 = new MyThread19_0(td);
?? ?MyThread19_1 mt1 = new MyThread19_1(td);
?? ?mt0.start();
?? ?mt1.start();
}
A begin time = 1443843271982A end time = 1443843273983B begin time = 1443843273983B end time = 1443843273983
?public class ThreadDomain20 {
????????public synchronized void otherMethod() {
????????????System.out.println("----------run--otherMethod");
????????}
????????public void doLongTask() {
????????????synchronized (this) {
????????????????for (int i = 0; i < 1000; i++) {
????????????????????System.out.println(
????????????????????????????"synchronized threadName = " + Thread.currentThread().getName() + ", i = " + (i + 1));
????????????????????try {
????????????????????????Thread.sleep(5);
????????????????????} catch (InterruptedException e) {
????????????????????????e.printStackTrace();
????????????????????}
????????????????}
????????????}
????????}
????}
public class MyThread20_0 extends Thread{
?? ?private ThreadDomain20 td;
?? ?public MyThread20_0(ThreadDomain20 td){
?? ??? ?this.td = td;
?? ?}
?? ?public void run(){
?? ??? ?td.doLongTask();
?? ?}
}
public class MyThread20_1 extends Thread{
?? ?private ThreadDomain20 td;
?? ?public MyThread20_1(ThreadDomain20 td){
?? ??? ?this.td = td;
?? ?}
?? ?public void run(){
?? ??? ?td.otherMethod();
?? ?}
}
public static void main(String[] args) throws Exception{
?? ?ThreadDomain20 td = new ThreadDomain20();
?? ?MyThread20_0 mt0 = new MyThread20_0(td);
?? ?MyThread20_1 mt1 = new MyThread20_1(td);
?? ?mt0.start();
?? ?Thread.sleep(100);
?? ?mt1.start();
}
...synchronized threadName = Thread-0, i = 995synchronized threadName = Thread-0, i = 996synchronized threadName = Thread-0, i = 997synchronized threadName = Thread-0, i = 998synchronized threadName = Thread-0, i = 999synchronized threadName = Thread-0, i = 1000----------run--otherMethod
...synchronized threadName = Thread-0, i = 16synchronized threadName = Thread-0, i = 17synchronized threadName = Thread-0, i = 18synchronized threadName = Thread-0, i = 19synchronized threadName = Thread-0, i = 20----------run--otherMethodsynchronized threadName = Thread-0, i = 21synchronized threadName = Thread-0, i = 22synchronized threadName = Thread-0, i = 23...
public class ThreadDomain21 {
????????private String userNameParam;
????????private String passwordParam;
????????private String anyString = new String();
????????public void setUserNamePassword(String userName, String password) {
????????????try {
????????????????synchronized (anyString) {
????????????????????System.out.println("線程名稱為:" + Thread.currentThread().getName() + "在 " + System.currentTimeMillis()?+ " 進(jìn)入同步代碼塊");
????????????????????userNameParam = userName;
????????????????????Thread.sleep(3000);
????????????????????passwordParam = password;
????????????????????System.out.println("線程名稱為:" + Thread.currentThread().getName() + "在 " + System.currentTimeMillis()?+ " 離開同步代碼塊");
????????????????}
????????????} catch (InterruptedException e) {
????????????????e.printStackTrace();
????????????}
????????}
????}
public class MyThread21_0 extends Thread{
?? ?private ThreadDomain21 td;
?? ?public MyThread21_0(ThreadDomain21 td){
?? ??? ?this.td = td;
?? ?}
?? ?public void run(){
?? ??? ?td.setUserNamePassword("A", "AA");
?? ?}
}
public class MyThread21_1 extends Thread{
?? ?private ThreadDomain21 td;
?? ?public MyThread21_1(ThreadDomain21 td){
?? ??? ?this.td = td;
?? ?}
?? ?public void run(){
?? ??? ?td.setUserNamePassword("B", "B");
?? ?}
}
public static void main(String[] args){
?? ?ThreadDomain21 td = new ThreadDomain21();
?? ?MyThread21_0 mt0 = new MyThread21_0(td);
?? ?MyThread21_1 mt1 = new MyThread21_1(td);
?? ?mt0.start();
?? ?mt1.start();
}
線程名稱為:Thread-0在 1443855101706 進(jìn)入同步代碼塊線程名稱為:Thread-0在 1443855104708 離開同步代碼塊線程名稱為:Thread-1在 1443855104708 進(jìn)入同步代碼塊線程名稱為:Thread-1在 1443855107708 離開同步代碼塊
public class MyObject {
????????public synchronized void speedPrintString() {
????????????System.out.println("speedPrintString__getLock time = " + System.currentTimeMillis() + ", run ThreadName = "?+ Thread.currentThread().getName());
????????????System.out.println("----------");
????????????System.out.println("speedPrintString__releaseLock time = " + System.currentTimeMillis()+ ", run ThreadName = " + Thread.currentThread().getName());
????????}
????}
ThreadDomain24中持有MyObject的引用:
public class ThreadDomain24 {
????????public void testMethod1(MyObject mo) {
????????????try {
????????????????synchronized (mo) {
????????????????????System.out.println("testMethod1__getLock time = " + System.currentTimeMillis()?+ ", run ThreadName = " + Thread.currentThread().getName());
????????????????????Thread.sleep(5000);
????????????????????System.out.println("testMethod1__releaseLock time = " + System.currentTimeMillis()?+ ", run ThreadName = " + Thread.currentThread().getName());
????????????????}
????????????} catch (InterruptedException e) {
????????????????e.printStackTrace();
????????????}?
????????}
????}
public class MyThread24_0 extends Thread{
?? ?private ThreadDomain24 td;
?? ?private MyObject mo;
?? ?public MyThread24_0(ThreadDomain24 td, MyObject mo){
?? ??? ?this.td = td;
?? ??? ?this.mo = mo;
?? ?}
?? ?public void run() {
?? ??? ?td.testMethod1(mo);
?? ?}
}
public class MyThread24_1 extends Thread{
?? ?private MyObject mo;
?? ?public MyThread24_1(MyObject mo){
?? ??? ?this.mo = mo;
?? ?}
?? ?public void run(){
?? ??? ?mo.speedPrintString();
?? ?}
}
public static void main(String[] args){
?? ?ThreadDomain24 td = new ThreadDomain24();
?? ?MyObject mo = new MyObject();
?? ?MyThread24_0 mt0 = new MyThread24_0(td, mo);
?? ?MyThread24_1 mt1 = new MyThread24_1(mo);
?? ?mt0.start();
?? ?mt1.start();
}
testMethod1__getLock time = 1443855939811, run ThreadName = Thread-0testMethod1__releaseLock time = 1443855944812, run ThreadName = Thread-0speedPrintString__getLock time = 1443855944812, run ThreadName = Thread-1----------speedPrintString__releaseLock time = 1443855944812, run ThreadName = Thread-1
文章來源地址http://www.zghlxwxcb.cn/news/detail-407800.html
文章來源:http://www.zghlxwxcb.cn/news/detail-407800.html
到了這里,關(guān)于【創(chuàng)作贏紅包】Java多線程:synchronized鎖方法塊的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!