溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

redis排序算法有哪些

發(fā)布時間:2021-11-17 09:32:22 來源:億速云 閱讀:136 作者:iii 欄目:大數(shù)據(jù)

本篇內容介紹了“redis排序算法有哪些”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

排序算法:

redis的sorted set類型的元素,都會關聯(lián)一個分數(shù),可以用于排序

  • Hacker News網站的排序算法:

適用于:只有點贊、對時間敏感

redis排序算法有哪些

  • Reddit 網站的排序算法:

適用于:有點贊點踩、流量較大的

redis排序算法有哪些

  • StackOverflow網站的排序算法:

適用于問答網站

redis排序算法有哪些

  • IMDB網站的排序算法:

適用于:電影評分排序

redis排序算法有哪些

多線程
  • Synchronized內置鎖:

加在方法上或代碼段上:

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();
		}
	}
}
  • BlockingQueue 同步隊列:

redis排序算法有哪些

add()方法如果隊列滿了會報錯,put()方法如果隊列滿了會等待,當隊列有空余則進行數(shù)據(jù)插入,offer()方法如果隊列滿會返回false,否則插入成功并返回true

examine--檢查

使用同步隊列可以實現(xiàn)之前的異步模型,但是和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() + ",消費者數(shù)據(jù):" + q.take());
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
  • 原子類AtomicXXXX:
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);
	}
}
  • 線程池任務框架:Executor
public class test4 {
	public static void testExecutor() throws InterruptedException {
//		ExecutorService service = Executors.newSingleThreadExecutor();//所有任務只有一個線程執(zhí)行
		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();
	}
}
  • 異步的Future:
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排序算法有哪些”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI