澳门新葡萄京娱乐场jsp只在首次加载时调用action实现代码

复制代码 代码如下:
List list=(List)request.getAttribute(“classlist”);
if(list==null){
response.sendRedirect(“class.do?method=doShow”);
}
%>

线程安全
代码1:
[code=”java”]@NotThreadSafe
class BadListHelper {
public List list = Collections.synchronizedList(new ArrayList;

我写的 乱序 洗牌 代码,请大家给点 意见,多多批评
[澳门新葡萄京娱乐场,code=”java”]

class.do是action名称。

public synchronized boolean putIfAbsent { boolean absent = !list.contains; if  list.add; return absent;}

import java.util.ArrayList;
import java.util.List;
import java.io.*;

}
@ThreadSafe
public class ImprovedList implements List {
private final List list;

public class Cards {
private List list;

/** * PRE: list argument is thread-safe. */public ImprovedList(List<T> list) { this.list = list; }public synchronized boolean putIfAbsent { boolean contains = list.contains; if  list.add; return !contains;}
public Cards() { list = new ArrayList<String>();}public void put(String card) { list.add;}public void get() { System.out.println; for (int i = 0; i < list.size { String cad = list.get; System.out.print(cad + ","); }}public static void main(String[] args) { String[] cards = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" }; String color[] = { "黑桃", "红桃", "梅花", "方块" }; Cards deck = new Cards(); for (int i = 0; i < cards.length; i++) { int num = 0; if (Math.random() <= 0.25) { num = 1; } if (Math.random() > 0.25 && Math.random() <= 0.5) { num = 2; } if (Math.random() > 0.5) { num = 3; } String suit = color[num]; String face = cards[ Math.ceil((Math.random() * 20 + 4) / 2)]; String card = suit + face; deck.put; } deck.get();}

}

}

[/code]
代码1不是线程安全的,而代码二是线程安全的
(最上面的是代码1,最后的代码是代码我)
二者的区别主要是final 修饰了list ,而final
如果指向引用的话,引用不可变,而所指向的引用是可以变化的
[b]
为什么代码1不是线程安全的,而代码2是线程安全的[/b]

[/code]
[b]问题补充:[/b]
[code=”java”]
//我修改后的代码,觉得还行

import java.util.ArrayList;
import java.util.List;

public class Cards {
private List list;

public Cards() { list = new ArrayList<String>();}public void put(String card) { list.add;}public void get() { System.out.println; String cad; for (int i = 0; i < list.size { cad = list.get; System.out.print(cad + ","); }}public static void main(String[] args) { String[] cards = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" }; String color[] = { "黑桃", "红桃", "梅花", "方块" }; Cards deck = new Cards(); double random; int num = 0; for (int i = 0; i < cards.length; i++) { random = Math.random(); if (random > 0.25 && random <= 0.5) { num = 1; } if (random > 0.5&&random<=0.75) { num = 2; } if (random > 0.75) { num = 3; } String suit = color[num]; String face = cards[ Math.ceil((Math.random() * 20 + 4) / 2)]; String card = suit + face; deck.put; } deck.get();}

}

[/code]
[b]问题补充:[/b]
[code=”java”]
import java.util.ArrayList;
import java.util.List;
// 应该是这个
public class Cards {
private List list;

public Cards() { list = new ArrayList<String>();}public void put(String card) { list.add;}public void get() { System.out.println; String cad; for (int i = 0; i < list.size { cad = list.get; System.out.print(cad + ","); }}public static void main(String[] args) { String[] cards = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" }; String color[] = { "黑桃", "红桃", "梅花", "方块" }; Cards deck = new Cards(); double random; int num = 0; String suit; String face; for (int i = 0; i < cards.length; i++) { random = Math.random(); if (random > 0.25 && random <= 0.5) { num = 1; } if (random > 0.5&&random<=0.75) { num = 2; } if (random > 0.75) { num = 3; } suit = color[num]; face = cards[ Math.ceil((Math.random() * 20 + 4) / 2)]; String card = suit + face; deck.put; } deck.get();}

}

[/code]

发表评论

电子邮件地址不会被公开。 必填项已用*标注