changeset 10:272cfe639e2f

build two jar
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 19 Dec 2017 14:26:35 +0900
parents e6ea4e10df98
children 4a4cc3c3b006
files build.gradle src/main/java/deadLockTest/TestDeadLock.java src/main/java/threadTest/Client.java src/main/java/threadTest/Server.java src/main/java/threadTest/TestThread.java
diffstat 5 files changed, 84 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/build.gradle	Fri Sep 23 12:12:55 2016 +0900
+++ b/build.gradle	Tue Dec 19 14:26:35 2017 +0900
@@ -19,4 +19,28 @@
             attributes  "Main-Class": "deadLockTest.TestDeadLock"
             attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': version
         }
+        baseName = "deadlockTest"
 }
+
+task threadTest(type: Jar) {
+    manifest {
+        attributes(
+                "Main-Class": 'threadTest.TestThread',
+                "Implementation-Title": 'TestThread',
+                "Implementation-Description": 'Quickstart',
+                "Implementation-Version": version,
+                "Assembly-Date": new java.util.Date().toString()
+        )
+    }
+    baseName = "threadTest"
+    // add all classes and resources produced from main source set
+     // (e.g. src/main/java, src/main/resources)
+    // add a single file; path is relative to project dir
+    // from "some/file.txt"
+    from(sourceSets.main.output) {
+        // filter to only include certain class files (Ant glob pattern)
+        include "threadTest/**"
+     }
+
+}
+
--- a/src/main/java/deadLockTest/TestDeadLock.java	Fri Sep 23 12:12:55 2016 +0900
+++ b/src/main/java/deadLockTest/TestDeadLock.java	Tue Dec 19 14:26:35 2017 +0900
@@ -5,6 +5,7 @@
 	Server plane = new Server("JAL");
 	Client t2 = new Client("Steve", plane,hotel);
 	Client t3 = new Client("Tedd", hotel,plane);
+	// Client t3 = new Client("Tedd", plane,hotel);
 	
 	public static void main(String arg[]) throws InterruptedException  {
 		TestDeadLock test = new TestDeadLock();
--- a/src/main/java/threadTest/Client.java	Fri Sep 23 12:12:55 2016 +0900
+++ b/src/main/java/threadTest/Client.java	Tue Dec 19 14:26:35 2017 +0900
@@ -2,12 +2,20 @@
 
 public class Client extends Thread {
 	Server server;
+	String name;
 	
-	Client(Server s) {
-		this.server = s;
+	Client(String name, Server s) {
+		this.name = name; this.server = s;
 	}
-	
-	public void run() {
-	    server.work();
+
+    public void run() {
+	    server.slow_work();
+    }
+
+    public void run1() {
+	    server.stringWork(name);
 	}
 }
+
+
+
--- a/src/main/java/threadTest/Server.java	Fri Sep 23 12:12:55 2016 +0900
+++ b/src/main/java/threadTest/Server.java	Tue Dec 19 14:26:35 2017 +0900
@@ -1,14 +1,18 @@
 package threadTest;
 
+import java.util.concurrent.atomic.AtomicInteger;
+
 public class Server {
 	int count = 0;
+	String sharedString  = "";
+	AtomicInteger atomicCount = new AtomicInteger();
 
 	void work()  {
 		//System.err.println("server-enter: count="+count);
 		count ++;
 		//System.err.println("server-leave: count="+count);
 	}
-	
+
 	void slow_work()  {
 		int tmp;
 		tmp = count ;
@@ -22,7 +26,7 @@
 
     private void sloop()  {
         try {
-            Thread.sleep(0);
+            Thread.sleep(1);
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
@@ -38,4 +42,29 @@
 		count = tmp ;
 		//System.err.println("server-leave: count="+count);
 	}
+
+
+    synchronized void stringWork(String s)  {
+        sharedString += s;
+    }
+
+	void atomicWork()  {
+		//System.err.println("server-enter: count="+count);
+		atomicCount.incrementAndGet();
+		//System.err.println("server-leave: count="+count);
+	}
+
+    void showResult() {
+        if (count != 2)
+            System.err.println("testThread: count = " + count);
+        assert (count == 2);
+    }
+
+    void showResultAtomc() {
+        assert (atomicCount.get() == 2);
+    }
+
+    void showResultSting() {
+        System.out.println(sharedString);
+    }
 }
--- a/src/main/java/threadTest/TestThread.java	Fri Sep 23 12:12:55 2016 +0900
+++ b/src/main/java/threadTest/TestThread.java	Tue Dec 19 14:26:35 2017 +0900
@@ -1,9 +1,11 @@
 package threadTest;
 
 public class TestThread {
-	Server t1 = new Server();
-	Client t2 = new Client(t1);
-	Client t3 = new Client(t1);
+
+	Server s0 = new Server();
+	Client t1 = new Client("a",s0);
+    Client t2 = new Client("b",s0);
+    Client t3 = new Client("c",s0);
 
 	public static void main(String arg[]) throws InterruptedException  {
 		//for(int i = 0; i<10000;i++) {
@@ -13,14 +15,14 @@
 	}
 	
 	public void test() throws InterruptedException {
-		t2.start();
-		t3.start();
-		t2.join();
-		t3.join();
-
-		if (t1.count!=2)
-			System.err.println("testThread: count = "+t1.count);
-		assert(t1.count==2);
-	}
-
+        t1.start();
+        t2.start();
+        t3.start();
+        t1.join();
+        t2.join();
+        t3.join();
+        s0.showResult();
+        // s0.showResultAtomc();
+        // s0.showResultSting();
+    }
 }