本文目錄一覽:
java版馬點右鍵一直叫
空手對著馬右鍵,失敗的話,馬會踢你下去,按右鍵騎不上去,只是馬會叫。然後繼續右鍵馬,建議你開第三人稱視角,一直對著馬右鍵,直到馬身上出現愛心特效,就說明馴服成功了,但這個時候只能騎馬不能操縱馬,手拿馬鞍對著馬右鍵,這樣就能騎著馬來回走了。
java 馬走日 兩點最小步數 用隊列
以下是兩個線程:
import java.util.*;
public class Thread_List_Operation {
//假設有這麼一個隊列
static List list = new LinkedList();
public static void main(String[] args) {
Thread t;
t = new Thread(new T1());
t.start();
t = new Thread(new T2());
t.start();
}
}
//線程T1,用來給list添加新元素
class T1 implements Runnable{
void getElemt(Object o){
Thread_List_Operation.list.add(o);
System.out.println(Thread.currentThread().getName() + “為隊列添加了一個元素”);
}
@Override
public void run() {
for (int i = 0; i 10; i++) {
getElemt(new Integer(1));
}
}
}
//線程T2,用來給list添加新元素
class T2 implements Runnable{
void getElemt(Object o){
Thread_List_Operation.list.add(o);
System.out.println(Thread.currentThread().getName() + “為隊列添加了一個元素”);
}
@Override
public void run() {
for (int i = 0; i 10; i++) {
getElemt(new Integer(1));
}
}
}
//結果(亂序)
Thread-0為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-0為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-1為隊列添加了一個元素
Thread-0為隊列添加了一個元素
Thread-0為隊列添加了一個元素
Thread-0為隊列添加了一個元素
Thread-0為隊列添加了一個元素
Thread-0為隊列添加了一個元素
Thread-0為隊列添加了一個元素
Thread-0為隊列添加了一個元素
Thread-0為隊列添加了一個元素
java 多線程 賽馬
package test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import sun.util.logging.resources.logging;
/**
* 2010-1-17 下午03:08:52 Piaolj
*/
public class Racing implements Runnable {
String str;
static Map horses = new TreeMap();// 定義一個全局的Map存放5個馬的名字和時間
static int count = 0;// 用於判斷縣城是否全部結束
static boolean flag = true;// 後面用while(flag)判斷count是否為0,如果有興趣,可以起另外一個線程完成此任務
public Racing(String string) {
// TODO Auto-generated constructor stub
this.str = string;
}
public static void main(String[] args) {
Racing ra1 = new Racing(“No.1 Horse”);
Racing ra2 = new Racing(“No.2 Horse”);
Racing ra3 = new Racing(“No.3 Horse”);
Racing ra4 = new Racing(“No.4 Horse”);
Racing ra5 = new Racing(“No.5 Horse”);
Racing checkingThread = new Racing(“checkingThread”);
Thread t1 = new Thread(ra1);
Thread t2 = new Thread(ra2);
Thread t3 = new Thread(ra3);
Thread t4 = new Thread(ra4);
Thread t5 = new Thread(ra5);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
while (flag) {
if (count == 0)// 所有線程結束
{
flag = false;
}
}
// 排序
List infoIds = new ArrayList(horses.entrySet());
Collections.sort(infoIds, new ComparatorMap.Entry() {
public int compare(Map.Entry o1, Map.Entry o2) {// 定義了比較的規則,因為這裡是按map的value排序的
Long tmp = Long.parseLong(o1.getValue().toString())
– Long.parseLong(o2.getValue().toString());
return tmp.intValue();
}
});
System.out.println(“輸出馬的名次:”);
System.out.println();
for (int i = 0; i infoIds.size(); i++) {
String id = infoIds.get(i).toString();
System.out.println(id);
}
}
public void run() {
// TODO Auto-generated method stub
int CircuitLength = 1000;
int breakpoint = 200;
int tmpint = 200;
long Withtime;
count = count + 1;// 執行了一個線程,正在執行的線程數加1
// System.out.println(Thread.currentThread().getId());
for (int i = 0; i CircuitLength + 1; i++) {
long start = System.currentTimeMillis();
if (i == breakpoint) {
int sleeping = (int) (Math.random() * 5000);
try {
Thread.sleep(sleeping);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
breakpoint = breakpoint + tmpint;
}
if (i == CircuitLength) {
System.out.print(str + “\t” + “\t”);
long end = System.currentTimeMillis();
Withtime = (end – start);
System.out.println(“With time is:\t” + Withtime);
// 當每匹馬跑完將馬的時間的馬的名稱放入map中
horses.put(str, Withtime);
}
}
count = count – 1;// 執行完了一個線程,線程數減1
}
}
時間比較少,寫的比較草,呵呵 加了點注釋,要是有問題可以發短消息
輸出結果:
No.4 Horse With time is: 888
No.3 Horse With time is: 3042
No.5 Horse With time is: 1921
No.2 Horse With time is: 4346
No.1 Horse With time is: 2831
輸出馬的名次:
No.4 Horse=888
No.5 Horse=1921
No.1 Horse=2831
No.3 Horse=3042
No.2 Horse=4346
原創文章,作者:MPZJP,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/316597.html