您好,登錄后才能下訂單哦!
本篇內容介紹了“redis排序算法有哪些”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
排序算法:
redis的sorted set類型的元素,都會關聯一個分數,可以用于排序
適用于:只有點贊、對時間敏感
適用于:有點贊點踩、流量較大的
適用于問答網站
適用于:電影評分排序
加在方法上或代碼段上:
public class Test1 { private static Object obj = new Object(); public static void synchronized1() throws InterruptedException { synchronized (obj) { for (int i = 0; i < 3; i++) { Thread.sleep(1000); System.out.println("鎖1:" + i + ",t:" + Thread.currentThread()); } } } public static void synchronized2() throws InterruptedException { synchronized (obj) { for (int i = 0; i < 3; i++) { Thread.sleep(1000); System.out.println("鎖2:" + i + ",t:" + Thread.currentThread()); } } } public static synchronized void synchronized3() throws InterruptedException { for (int i = 0; i < 3; i++) { Thread.sleep(1000); System.out.println("鎖1:" + i + ",t:" + Thread.currentThread()); } } public static synchronized void synchronized4() throws InterruptedException { for (int i = 0; i < 3; i++) { Thread.sleep(1000); System.out.println("鎖2:" + i + ",t:" + Thread.currentThread()); } } public static void main(String[] args) throws InterruptedException { for (int i = 0; i < 5; i++) { new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { // synchronized1(); // synchronized2(); synchronized3(); synchronized4();//效果一樣 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); } } }
add()方法如果隊列滿了會報錯,put()方法如果隊列滿了會等待,當隊列有空余則進行數據插入,offer()方法如果隊列滿會返回false,否則插入成功并返回true
examine--檢查
使用同步隊列可以實現之前的異步模型,但是和redis的List相比,同步隊列無法適用于分布式
public class test2 { public static void main(String[] args) throws Exception { Producer.q.put("like"); // Producer.q.put("dislike"); // Producer.q.add("dislike"); Producer.q.offer("dislike"); new Thread(new Consumer(Producer.q), "t1").start(); new Thread(new Consumer(Producer.q), "t2").start(); } } class Producer { public static BlockingQueue<String> q = new ArrayBlockingQueue<String>(10); } class Consumer implements Runnable { private BlockingQueue<String> q; Consumer(BlockingQueue<String> q) { this.q = q; } @Override public void run() { try { System.out.println(Thread.currentThread().getName() + ",消費者數據:" + q.take()); Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } }
public class test3 { private static int a = 0; private static AtomicInteger b = new AtomicInteger(0); public static void main(String[] args) throws InterruptedException { for (int i = 0; i < 50; i++) { new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(1000); a++; b.incrementAndGet(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); } Thread.sleep(3000);//等50個線程運行完 System.out.println(a); System.out.println(b); } }
public class test4 { public static void testExecutor() throws InterruptedException { // ExecutorService service = Executors.newSingleThreadExecutor();//所有任務只有一個線程執行 ExecutorService service = Executors.newFixedThreadPool(2);// 每個任務都分配一個線程 service.submit(new Runnable() { @Override public void run() { for (int i = 0; i < 5; ++i) { try { Thread.sleep(1000); System.out.println("Execute1 " + i); } catch (InterruptedException e) { e.printStackTrace(); } } } }); service.submit(new Runnable() { @Override public void run() { for (int i = 0; i < 5; ++i) { try { Thread.sleep(1000); System.out.println("Execute2 " + i); } catch (InterruptedException e) { e.printStackTrace(); } } } }); service.shutdown(); // 輪詢,service是否停止 while (!service.isTerminated()) { Thread.sleep(1000); System.out.println("ExecutorService正在運行,Wait for termination."); } } public static void main(String[] args) throws InterruptedException { testExecutor(); } }
public class test5 { public static void testfuture() throws Exception { ExecutorService service = Executors.newFixedThreadPool(2); Future<Integer> future = service.submit(new Callable<Integer>() { @Override public Integer call() throws Exception { System.out.println("開始計算任務。。。"); Thread.sleep(3000); System.out.println("開始返回結果。。。"); return 100; } }); service.isShutdown(); System.out.println("等待的結果:" + future.get(2000, TimeUnit.SECONDS)); } public static void main(String[] args) throws Exception { testfuture(); } }
“redis排序算法有哪些”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。