لقد ألقيت نظرة اليوم على برنامج Java المتزامن وكتبت برنامج إدخال وقمت بتعيين أولوية الخيط.
class Elements Runnable{ public static int id = 0; public int CutDown = 5; public void setPriority(int Priority){ this.priority = Priority; } public int getPriority(){ return this.priority; void run(){ Thread.currentThread().setPriority(priority); int threadId = id++; 0){ double d = 1.2; while(d < 10000) d = d + (Math.E + Math.PI)/d; System.out.println("#" + threadId + "(" + CutDown + ") "); } }}public class Basic { public static void main(String args[]){ for(int i = 0; i < 10; i++){ العنصر e = جديد Elem(); if(i == 0 ) e.setPriority(Thread.MAX_PRIORITY); else e.setPriority(Thread.MIN_PRIORITY);
نظرًا لأن الجهاز قوي جدًا، لم أر التأثير المتزامن في البداية، وشعرت أنه تم تنفيذه بالتسلسل، لذلك تمت إضافة عمليات الفاصلة العائمة في المنتصف لتأخير الوقت.
بالطبع، يمكن استخدام المنفذين لإدارة سلاسل الرسائل في الوظيفة الرئيسية.
import java.util.concurrent.*;class Elem Implements Runnable{ public static int id = 0; public int gotPriority; ){ return this.priority } public void run(){ Thread.currentThread().setPriority(priority); int threadId = id++; while(cutDown-- > 0){ double d = 1.2; while(d < 10000) d = d + (Math.E + Math.PI)/d; "(" + CutDown + ")"); public class Basic { public static void main(String args[]){// for(int i = 0; i < 10; i++){// Elem e = new Elem();// if(i == 0 )// e.setPriority(Thread.MAX_PRIORITY);// else// e.setPriority(Thread.MIN_PRIORITY);/ /Thread t = new Thread(e);// t.start();// } ExecutorService exec = Executors.newCachedThreadPool(); for(int i = 0; i < 10; i++){ Elem e = new Elem(); if(i == 0 ) e.setPriority(Thread.MAX_PRIORITY); .MIN_PRIORITY); exec.execute(e);